/*      */ package com.newcrm.db;
/*      */ 
/*      */ import com.newcrm.bean.CustomerInfo;
/*      */ import com.newcrm.bean.CustomerSearchCondition;
/*      */ import com.newcrm.bean.ExtInfo;
/*      */ import com.newcrm.bean.Tag;
/*      */ import com.newcrm.bean.User;
/*      */ import com.newcrm.exception.CustomerIsExistException;
/*      */ import com.newcrm.exception.DBException;
/*      */ import com.newcrm.util.Util;
/*      */ import java.sql.Connection;
/*      */ import java.sql.SQLException;
/*      */ import java.util.ArrayList;
/*      */ import java.util.Calendar;
/*      */ import java.util.Date;
/*      */ import java.util.GregorianCalendar;
/*      */ import java.util.Iterator;
/*      */ import java.util.List;
/*      */ import java.util.Map;
/*      */ import org.apache.commons.dbutils.DbUtils;
/*      */ import org.apache.commons.dbutils.GenKeyQueryRunner;
/*      */ import org.apache.commons.dbutils.MyMapHandler;
/*      */ import org.apache.commons.dbutils.MyMapListHandler;
/*      */ import org.apache.commons.dbutils.QueryRunner;
/*      */ import org.apache.commons.dbutils.handlers.ArrayHandler;
/*      */ import org.apache.commons.dbutils.handlers.MapListHandler;
/*      */ import org.apache.commons.dbutils.handlers.ScalarHandler;
/*      */ import org.apache.log4j.Logger;
/*      */ 
/*      */ public class CustomerManager
/*      */ {
/*   35 */   private static Logger logger = Logger.getLogger(CustomerManager.class
/*   36 */     .getName());
/*      */ 
/*      */   public int clearImportCustomers(int importId, boolean isAdmin, String loginUserId, int companyId)
/*      */     throws DBException, SQLException
/*      */   {
/*   48 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*   49 */     if (conn == null) {
/*   50 */       throw new DBException();
/*      */     }
/*      */ 
/*   53 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*   57 */       int result = 0;
/*   58 */       if (isAdmin)
/*   59 */         result = runner.update(conn, "DELETE FROM T_Import_Info WHERE CompanyId = ? AND ImportId = ?", new Object[] { 
/*   60 */           Integer.valueOf(companyId), Integer.valueOf(importId) });
/*      */       else {
/*   62 */         result = runner.update(conn, "DELETE FROM T_Import_Info WHERE CompanyId = ? AND ImportId = ? AND CreateUserId = ? ", new Object[] { 
/*   63 */           Integer.valueOf(companyId), Integer.valueOf(importId), loginUserId });
/*      */       }
/*      */ 
/*   66 */       if (result <= 0) {
/*   67 */         return 0;
/*      */       }
/*      */ 
/*   71 */       runner.update(conn, "DELETE FROM T_Customer_Ext_Info WHERE CustomerId IN (SELECT CustomerId FROM T_Customer_Info WHERE ImportId = ?)", Integer.valueOf(importId));
/*   72 */       runner.update(conn, "UPDATE T_Customer_Info SET DeleteFlag = 1, IndexFlag = 0 WHERE ImportId = ?", Integer.valueOf(importId));
/*      */ 
/*   74 */       return result;
/*      */     } finally {
/*      */       try {
/*   77 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*   79 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int[] importCustomer(Connection conn, String companyName, int categoryId, String peopleName, String title, String companyNote, List<ExtInfo> companyExtInfos, List<ExtInfo> peopleExtInfos, String userId, int companyId, int visible, String permissionsId, boolean isOverwrite, int importId)
/*      */     throws DBException, CustomerIsExistException
/*      */   {
/*  121 */     if (conn == null) {
/*  122 */       throw new DBException();
/*      */     }
/*      */ 
/*  125 */     if ((companyName == null) || (companyName.trim().length() == 0)) {
/*  126 */       return importPeople(conn, categoryId, peopleName, title, 
/*  127 */         companyNote, companyExtInfos, peopleExtInfos, userId, 
/*  128 */         companyId, visible, permissionsId, isOverwrite, importId);
/*      */     }
/*      */ 
/*      */     try
/*      */     {
/*  135 */       QueryRunner runner2 = new QueryRunner();
/*  136 */       Object[] result = (Object[])runner2.query(conn, 
/*  137 */         "SELECT CustomerId, Visible, PermissionsId, CategoryId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND Name = ? AND Type = 0 ", new ArrayHandler(), new Object[] { 
/*  138 */         Integer.valueOf(companyId), companyName });
/*      */       int customerId;
/*  140 */       if (result != null) {
/*  141 */         if (isOverwrite)
/*      */         {
/*  143 */           throw new CustomerIsExistException(Util.str2Int(result[0]));
/*      */         }
/*      */ 
/*  148 */         int parentCustomerId = Util.str2Int(result[0]);
/*      */ 
/*  154 */         mergeImportCustomerExtInfo(conn, companyId, 
/*  155 */           parentCustomerId, companyExtInfos, userId);
/*      */ 
/*  163 */         int parentVisible = Util.str2Int(result[1]);
/*  164 */         String parentPermissionsId = (String)result[2];
/*  165 */         categoryId = Util.str2Int(result[3]);
/*      */ 
/*  168 */         if ((peopleName != null) && (peopleName.trim().length() != 0))
/*      */         {
/*  173 */           Object[] result2 = (Object[])runner2.query(conn, 
/*  174 */             "SELECT CustomerId FROM T_Customer_Info WHERE DeleteFlag = 0 AND ParentCustomerId = ? AND Name = ? AND Type = 1 ", 
/*  175 */             new ArrayHandler(), new Object[] { Integer.valueOf(parentCustomerId), 
/*  176 */             peopleName });
/*      */ 
/*  181 */           if (result2 != null) {
/*  182 */             int peopleCustomerId = ((Integer)result2[0]).intValue();
/*      */ 
/*  184 */             mergeImportCustomerExtInfo(conn, companyId, 
/*  185 */               peopleCustomerId, peopleExtInfos, userId);
/*      */ 
/*  187 */             throw new CustomerIsExistException(peopleCustomerId);
/*      */           }
/*      */ 
/*  191 */           customerId = 0;
/*  192 */           GenKeyQueryRunner runner = new GenKeyQueryRunner(
/*  193 */             new ScalarHandler(), new String[] { "CustomerId" });
/*  194 */           runner.update(conn, "INSERT INTO T_Customer_Info (Name,Type,CategoryId,Title,Note,Visible,PermissionsId,CompanyId,CreateUserId,UpdateUserId,CreateDate,UpdateDate,AssignDate,ParentCustomerId,ImportId,LastNoteId,DeleteFlag) VALUES (?,?,?,?,?,?,?,?,?,?,NOW(),NOW(),NOW(),?,?,-1,0)", new Object[] { peopleName, 
/*  195 */             Integer.valueOf(1), Integer.valueOf(categoryId), title, "", 
/*  196 */             Integer.valueOf(parentVisible), parentPermissionsId, Integer.valueOf(companyId), 
/*  197 */             userId, userId, Integer.valueOf(parentCustomerId), Integer.valueOf(importId) });
/*  198 */           customerId = ((Long)runner.getGeneratedKeys())
/*  199 */             .intValue();
/*      */ 
/*  201 */           if ((peopleExtInfos != null) && (peopleExtInfos.size() > 0)) {
/*  202 */             List baseParams = new ArrayList();
/*  203 */             for (ExtInfo extInfo : peopleExtInfos) {
/*  204 */               List info = createInsertExtInfoList(
/*  205 */                 companyId, customerId, extInfo);
/*  206 */               baseParams.add(info.toArray(new Object[0]));
/*      */             }
/*  208 */             runner.batch(conn, "INSERT INTO T_Customer_Ext_Info (CompanyId,CustomerId,Type,Info1,Info2,Info3,Info4,CreateDate) VALUES (?,?,?,?,?,?,?,NOW())", 
/*  209 */               (Object[][])baseParams.toArray(new Object[0][]));
/*      */           }
/*  211 */           return new int[] { parentCustomerId, customerId };
/*      */         }
/*      */ 
/*  216 */         throw new CustomerIsExistException(parentCustomerId);
/*      */       }
/*      */ 
/*  222 */       GenKeyQueryRunner runner = new GenKeyQueryRunner(
/*  223 */         new ScalarHandler(), new String[] { "CustomerId" });
/*      */ 
/*  225 */       runner.update(conn, "INSERT INTO T_Customer_Info (Name,Type,CategoryId,Title,Note,Visible,PermissionsId,CompanyId,CreateUserId,UpdateUserId,CreateDate,UpdateDate,AssignDate,ParentCustomerId,ImportId,LastNoteId,DeleteFlag) VALUES (?,?,?,?,?,?,?,?,?,?,NOW(),NOW(),NOW(),?,?,-1,0)", new Object[] { companyName, 
/*  226 */         Integer.valueOf(0), Integer.valueOf(categoryId), "", companyNote, Integer.valueOf(visible), 
/*  227 */         permissionsId, Integer.valueOf(companyId), userId, userId, Integer.valueOf(-1), Integer.valueOf(importId) });
/*      */ 
/*  229 */       int parentCustomerId = ((Long)runner.getGeneratedKeys())
/*  230 */         .intValue();
/*      */       List info;
/*  231 */       if ((companyExtInfos != null) && (companyExtInfos.size() > 0)) {
/*  232 */         List baseParams = new ArrayList();
/*  233 */         for (ExtInfo extInfo : companyExtInfos) {
/*  234 */           info = createInsertExtInfoList(companyId, 
/*  235 */             parentCustomerId, extInfo);
/*  236 */           baseParams.add(info.toArray(new Object[0]));
/*      */         }
/*  238 */         runner.batch(conn, "INSERT INTO T_Customer_Ext_Info (CompanyId,CustomerId,Type,Info1,Info2,Info3,Info4,CreateDate) VALUES (?,?,?,?,?,?,?,NOW())", 
/*  239 */           (Object[][])baseParams.toArray(new Object[0][]));
/*      */       }
/*  241 */       customerId = 0;
/*  242 */       if ((peopleName != null) && (peopleName.trim().length() > 0)) {
/*  243 */         runner.update(conn, "INSERT INTO T_Customer_Info (Name,Type,CategoryId,Title,Note,Visible,PermissionsId,CompanyId,CreateUserId,UpdateUserId,CreateDate,UpdateDate,AssignDate,ParentCustomerId,ImportId,LastNoteId,DeleteFlag) VALUES (?,?,?,?,?,?,?,?,?,?,NOW(),NOW(),NOW(),?,?,-1,0)", new Object[] { peopleName, 
/*  244 */           Integer.valueOf(1), Integer.valueOf(categoryId), title, "", Integer.valueOf(visible), 
/*  245 */           permissionsId, Integer.valueOf(companyId), userId, userId, 
/*  246 */           Integer.valueOf(parentCustomerId), Integer.valueOf(importId) });
/*  247 */         customerId = ((Long)runner.getGeneratedKeys()).intValue();
/*  248 */         if ((peopleExtInfos != null) && (peopleExtInfos.size() > 0)) {
/*  249 */           List baseParams = new ArrayList();
/*  250 */           for (ExtInfo extInfo : peopleExtInfos) {
/*  251 */             info = createInsertExtInfoList(companyId,
/*  252 */               customerId, extInfo);
/*  253 */             baseParams.add(info.toArray(new Object[0]));
/*      */           }
/*      */ 
/*  256 */           runner.batch(conn, "INSERT INTO T_Customer_Ext_Info (CompanyId,CustomerId,Type,Info1,Info2,Info3,Info4,CreateDate) VALUES (?,?,?,?,?,?,?,NOW())", 
/*  257 */             (Object[][])baseParams.toArray(new Object[0][]));
/*      */         }
/*      */       }
/*      */ 
/*  261 */       return new int[] { parentCustomerId, customerId };
/*      */     }
/*      */     catch (SQLException e) {
/*  264 */       logger.warn("importCustomer", e);
/*      */     }
/*  266 */     return null;
/*      */   }
/*      */ 
/*      */   private void setUpdateDate(Connection conn, int companyId, String userId, int customerId)
/*      */   {
/*  279 */     if ((conn == null) || (customerId <= 0) || (companyId <= 0)) {
/*  280 */       return;
/*      */     }
/*  282 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/*  284 */       runner.update(conn, "UPDATE T_Customer_Info SET UpdateDate = NOW(), UpdateUserId = ? WHERE CompanyId = ? AND CustomerId = ?", new Object[] { userId, 
/*  285 */         Integer.valueOf(companyId), Integer.valueOf(customerId) });
/*      */     } catch (SQLException e) {
/*  287 */       logger.warn("setUpdateDate failed", e);
/*      */     }
/*      */   }
/*      */ 
/*      */   private void mergeImportCustomerExtInfo(Connection conn, int companyId, int customerId, List<ExtInfo> extInfos, String userId)
/*      */   {
/*  308 */     if ((conn == null) || (customerId <= 0) || (extInfos == null) || 
/*  309 */       (extInfos.size() == 0)) {
/*  310 */       return;
/*      */     }
/*      */ 
/*  313 */     List insertParams = new ArrayList();
/*  314 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  317 */       List result = (List)runner.query(conn, 
/*  318 */         "SELECT * FROM T_Customer_Ext_Info WHERE CustomerId = ? ORDER BY Type ASC, Info3 ASC, CreateDate ASC", new MyMapListHandler(), new Object[] { Integer.valueOf(customerId) });
/*      */ 
/*  320 */       if (result == null)
/*      */       {
/*  322 */         for (ExtInfo extInfo : extInfos) {
/*  323 */           List newInfo = createInsertExtInfoList(companyId, 
/*  324 */             customerId, extInfo);
/*  325 */           insertParams.add(newInfo.toArray(new Object[0]));
/*      */         }
/*      */ 
/*  328 */         runner.batch(conn, "INSERT INTO T_Customer_Ext_Info (CompanyId,CustomerId,Type,Info1,Info2,Info3,Info4,CreateDate) VALUES (?,?,?,?,?,?,?,NOW())", 
/*  329 */           (Object[][])insertParams.toArray(new Object[0][]));
/*      */ 
/*  331 */         setUpdateDate(conn, companyId, userId, customerId);
/*      */ 
/*  333 */         return;
/*      */       }
/*      */ 
/*  337 */       List updateParams = new ArrayList();
/*      */ 
/*  340 */       Object dbExtInfos = new ArrayList();
/*  341 */       for (Object extInfoO : result) {
                    Map extInfo = (Map)extInfoO;
/*  342 */         ExtInfo info = new ExtInfo();
/*  343 */         info.id = Util.str2Int(extInfo.get("CustomerExtId"));
/*  344 */         info.type = Util.str2Int(extInfo.get("Type"));
/*  345 */         info.info1 = ((String)extInfo.get("Info1"));
/*  346 */         info.info2 = ((String)extInfo.get("Info2"));
/*  347 */         info.info3 = ((String)extInfo.get("Info3"));
/*  348 */         info.info4 = ((String)extInfo.get("Info4"));
/*  349 */         ((List)dbExtInfos).add(info);
/*      */       }
/*      */ 
/*  352 */       for (ExtInfo extInfo : extInfos) {
/*  353 */         if (isExistExtInfo((List)dbExtInfos, extInfo))
/*      */         {
/*  355 */           List updateInfo = createUpdateExtInfoList(extInfo);
/*  356 */           updateParams.add(updateInfo.toArray(new Object[0]));
/*      */         }
/*      */         else
/*      */         {
/*  361 */           List newInfo = createInsertExtInfoList(companyId, 
/*  362 */             customerId, extInfo);
/*  363 */           insertParams.add(newInfo.toArray(new Object[0]));
/*      */         }
/*      */       }
/*      */ 
/*  367 */       if ((updateParams != null) && (updateParams.size() > 0)) {
/*  368 */         runner.batch(conn, "UPDATE T_Customer_Ext_Info SET Info1 = ?, Info2 = ?, Info3 = ?, Info4 = ? WHERE CustomerExtId = ?", 
/*  369 */           (Object[][])updateParams.toArray(new Object[0][]));
/*      */ 
/*  371 */         setUpdateDate(conn, companyId, userId, customerId);
/*      */       }
/*      */ 
/*  375 */       if ((insertParams != null) && (insertParams.size() > 0)) {
/*  376 */         runner.batch(conn, "INSERT INTO T_Customer_Ext_Info (CompanyId,CustomerId,Type,Info1,Info2,Info3,Info4,CreateDate) VALUES (?,?,?,?,?,?,?,NOW())", 
/*  377 */           (Object[][])insertParams.toArray(new Object[0][]));
/*      */ 
/*  379 */         setUpdateDate(conn, companyId, userId, customerId);
/*      */       }
/*      */     } catch (SQLException e) {
/*  382 */       logger.warn("mergeImportCustomerExtInfo failed", e);
/*      */     }
/*      */   }
/*      */ 
/*      */   private boolean isExistExtInfo(List<ExtInfo> dbExtInfos, ExtInfo extInfo)
/*      */   {
/*  394 */     if ((dbExtInfos == null) || (dbExtInfos.size() == 0)) {
/*  395 */       return false;
/*      */     }
/*      */ 
/*  398 */     if (extInfo == null) {
/*  399 */       return false;
/*      */     }
/*      */ 
/*  402 */     boolean flag = false;
/*  403 */     for (ExtInfo item : dbExtInfos) {
/*  404 */       if (item.type == extInfo.type)
/*      */       {
/*  409 */         if (extInfo.type >= 99) {
/*  410 */           extInfo.id = item.id;
/*  411 */           return true;
/*      */         }
/*      */ 
/*  415 */         flag = extInfo.info1.equals(item.info1);
/*  416 */         if (flag) {
/*  417 */           extInfo.id = item.id;
/*  418 */           return true;
/*      */         }
/*      */       }
/*      */     }
/*  422 */     return false;
/*      */   }
/*      */ 
/*      */   private List<Object> createInsertExtInfoList(int companyId, int customerId, ExtInfo extInfo)
/*      */   {
/*  469 */     List insertInfo = new ArrayList();
/*  470 */     insertInfo.add(Integer.valueOf(companyId));
/*  471 */     insertInfo.add(Integer.valueOf(customerId));
/*  472 */     insertInfo.add(Integer.valueOf(extInfo.type));
/*  473 */     insertInfo.add(extInfo.info1);
/*  474 */     insertInfo.add(extInfo.info2);
/*  475 */     insertInfo.add(extInfo.info3);
/*  476 */     insertInfo.add(extInfo.info4);
/*  477 */     return insertInfo;
/*      */   }
/*      */ 
/*      */   private List<Object> createUpdateExtInfoList(ExtInfo extInfo)
/*      */   {
/*  487 */     List insertInfo = new ArrayList();
/*  488 */     insertInfo.add(extInfo.info1);
/*  489 */     insertInfo.add(extInfo.info2);
/*  490 */     insertInfo.add(extInfo.info3);
/*  491 */     insertInfo.add(extInfo.info4);
/*  492 */     insertInfo.add(Integer.valueOf(extInfo.id));
/*  493 */     return insertInfo;
/*      */   }
/*      */ 
/*      */   public int[] importPeople(Connection conn, int categoryId, String peopleName, String title, String companyNote, List<ExtInfo> companyExtInfos, List<ExtInfo> peopleExtInfos, String userId, int companyId, int visible, String permissionsId, boolean isOverwrite, int importId)
/*      */     throws DBException, CustomerIsExistException
/*      */   {
/*  523 */     if (conn == null) {
/*  524 */       throw new DBException();
/*      */     }
/*      */ 
/*  527 */     if ((peopleName == null) || (peopleName.trim().length() == 0)) {
/*  528 */       return null;
/*      */     }
/*      */     try
/*      */     {
/*  532 */       GenKeyQueryRunner runner = new GenKeyQueryRunner(
/*  533 */         new ScalarHandler(), new String[] { "CustomerId" });
/*      */ 
/*  535 */       int parentCustomerId = -1;
/*  536 */       int customerId = 0;
/*      */ 
/*  538 */       runner.update(conn, "INSERT INTO T_Customer_Info (Name,Type,CategoryId,Title,Note,Visible,PermissionsId,CompanyId,CreateUserId,UpdateUserId,CreateDate,UpdateDate,AssignDate,ParentCustomerId,ImportId,LastNoteId,DeleteFlag) VALUES (?,?,?,?,?,?,?,?,?,?,NOW(),NOW(),NOW(),?,?,-1,0)", new Object[] { peopleName, 
/*  539 */         Integer.valueOf(1), Integer.valueOf(categoryId), title, companyNote, Integer.valueOf(visible), 
/*  540 */         permissionsId, Integer.valueOf(companyId), userId, userId, Integer.valueOf(parentCustomerId), 
/*  541 */         Integer.valueOf(importId) });
/*      */ 
/*  543 */       customerId = ((Long)runner.getGeneratedKeys()).intValue();
/*  544 */       if ((peopleExtInfos != null) && (peopleExtInfos.size() > 0)) {
/*  545 */         List baseParams = new ArrayList();
/*  546 */         for (ExtInfo extInfo : peopleExtInfos) {
/*  547 */           List info = createInsertExtInfoList(companyId, 
/*  548 */             customerId, extInfo);
/*  549 */           baseParams.add(info.toArray(new Object[0]));
/*      */         }
/*      */ 
/*  552 */         runner.batch(conn, "INSERT INTO T_Customer_Ext_Info (CompanyId,CustomerId,Type,Info1,Info2,Info3,Info4,CreateDate) VALUES (?,?,?,?,?,?,?,NOW())", 
/*  553 */           (Object[][])baseParams.toArray(new Object[0][]));
/*      */       }
/*      */ 
/*  557 */       return new int[] { parentCustomerId, customerId };
/*      */     }
/*      */     catch (SQLException e) {
/*  560 */       logger.warn("importCustomer", e);
/*      */     }
/*  562 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> checkExistTel(int companyId, String tel)
/*      */   {
/*  574 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  575 */     if (conn == null)
/*  576 */       return null;
/*      */     try
/*      */     {
/*  579 */       QueryRunner runner = new QueryRunner();
/*  580 */       return (List)runner.query(conn, "SELECT B.Name FROM T_Customer_Ext_Info AS A LEFT JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE B.DeleteFlag = 0 AND B.CompanyId = ? AND A.Type = ? AND A.Info1 = ?", 
/*  581 */         new MyMapListHandler(), new Object[] { Integer.valueOf(companyId), 
/*  582 */         Integer.valueOf(1), tel });
/*      */     } catch (SQLException e) {
/*  584 */       logger.warn("check company tel exist failed", e);
/*      */     } finally {
/*      */       try {
/*  587 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  589 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*  592 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> checkExistQQ(int companyId, String qq)
/*      */   {
/*  603 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  604 */     if (conn == null)
/*  605 */       return null;
/*      */     try
/*      */     {
/*  608 */       QueryRunner runner = new QueryRunner();
/*  609 */       return (List)runner.query(conn, "SELECT B.Name FROM T_Customer_Ext_Info AS A LEFT JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE B.DeleteFlag = 0 AND B.CompanyId = ? AND A.Type = ? AND A.Info1 = ? AND Info3 = ?", 
/*  610 */         new MyMapListHandler(), new Object[] { Integer.valueOf(companyId), 
/*  611 */         Integer.valueOf(3), qq, "QQ" });
/*      */     } catch (SQLException e) {
/*  613 */       logger.warn("check company qq exist failed", e);
/*      */     } finally {
/*      */       try {
/*  616 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  618 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*  621 */     return null;
/*      */   }
/*      */ 
/*      */   public boolean isExistCompanyName(int companyId, String name)
/*      */     throws DBException
/*      */   {
/*  634 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  635 */     if (conn == null)
/*  636 */       throw new DBException();
/*      */     try
/*      */     {
/*  639 */       QueryRunner runner2 = new QueryRunner();
/*  640 */       Object[] result = (Object[])runner2.query(conn, "SELECT CustomerId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND Name = ? AND Type = 0 ", 
/*  641 */         new ArrayHandler(), new Object[] { Integer.valueOf(companyId), name });
/*  642 */       if (result != null)
/*  643 */         return true;
/*      */     }
/*      */     catch (SQLException e) {
/*  646 */       logger.warn("add customer failed", e);
/*      */     } finally {
/*      */       try {
/*  649 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  651 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */     try
/*      */     {
/*  649 */       DbUtils.close(conn);
/*      */     } catch (SQLException e) {
/*  651 */       logger.warn("close conn failed", e);
/*      */     }
/*      */ 
/*  654 */     return false;
/*      */   }
/*      */ 
/*      */   public int getCustomerId(int companyId, String name)
/*      */     throws DBException
/*      */   {
/*  666 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  667 */     if (conn == null)
/*  668 */       throw new DBException();
/*      */     try
/*      */     {
/*  671 */       QueryRunner runner2 = new QueryRunner();
/*  672 */       Object[] result = (Object[])runner2.query(conn, "SELECT CustomerId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND Name = ? AND Type = 0 ", 
/*  673 */         new ArrayHandler(), new Object[] { Integer.valueOf(companyId), name });
/*  674 */       if (result != null) {
/*  675 */         return Util.str2Int(result[0]);
/*      */       }
/*  677 */       return 0;
/*      */     }
/*      */     catch (SQLException e) {
/*  680 */       logger.warn("add customer failed", e);
/*      */     } finally {
/*      */       try {
/*  683 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  685 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*  688 */     return 0;
/*      */   }
/*      */ 
/*      */   public int addCompany(String name, int categoryId, String note, int visible, String permissionsId, String shareUserIds, String shareGroupIds, String tagIds, List<ExtInfo> extInfos, String userId, int companyId)
/*      */   {
/*  712 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  713 */     if (conn == null) {
/*  714 */       return 0;
/*      */     }
/*      */     try
/*      */     {
/*  718 */       QueryRunner runner2 = new QueryRunner();
/*  719 */       Object[] result = (Object[])runner2.query(conn, "SELECT CustomerId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND Name = ? AND Type = 0 ", 
/*  720 */         new ArrayHandler(), new Object[] { Integer.valueOf(companyId), name });
/*      */       int k;
/*  721 */       if (result != null) {
/*  722 */         int customerId = Util.str2Int(result[0]);
/*  723 */         return customerId * -1;
/*      */       }
/*      */ 
/*  726 */       GenKeyQueryRunner runner = new GenKeyQueryRunner(
/*  727 */         new ScalarHandler(), new String[] { "CustomerId" });
/*  728 */       runner.update(conn, "INSERT INTO T_Customer_Info (Name,Type,CategoryId,Title,Note,Visible,PermissionsId,CompanyId,CreateUserId,UpdateUserId,CreateDate,UpdateDate,AssignDate,ParentCustomerId,ImportId,LastNoteId,DeleteFlag) VALUES (?,?,?,?,?,?,?,?,?,?,NOW(),NOW(),NOW(),?,?,-1,0)", new Object[] { name, 
/*  729 */         Integer.valueOf(0), Integer.valueOf(categoryId), "", note, Integer.valueOf(visible), 
/*  730 */         permissionsId, Integer.valueOf(companyId), userId, userId, Integer.valueOf(-1), Integer.valueOf(0) });
/*      */ 
/*  732 */       int customerId = ((Long)runner.getGeneratedKeys()).intValue();
/*  733 */       if (customerId <= 0) {
/*  734 */         return customerId;
/*      */       }
/*      */ 
/*  738 */       if ((tagIds != null) && (tagIds.trim().length() > 0)) {
/*  739 */         List baseParams = new ArrayList();
/*  740 */         for (String id : tagIds.split(",")) {
/*  741 */           List info = new ArrayList();
/*  742 */           int tagId = Util.str2Int(id);
/*  743 */           if (tagId > 0)
/*      */           {
/*  746 */             info.add(Integer.valueOf(customerId));
/*  747 */             info.add(Integer.valueOf(tagId));
/*  748 */             info.add(Integer.valueOf(companyId));
/*  749 */             info.add(userId);
/*  750 */             baseParams.add(info.toArray(new Object[0]));
/*      */           }
/*      */         }
/*  752 */         runner.batch(conn, "INSERT INTO T_Customer_Tag_Info (CustomerId,TagId,CompanyId,CreateDate,CreateUserId) VALUES (?,?,?,NOW(),?)", 
/*  753 */           (Object[][])baseParams.toArray(new Object[0][]));
/*      */       }
/*      */ 
/*  757 */       if ((extInfos != null) && (extInfos.size() > 0)) {
/*  758 */         List baseParams = new ArrayList();
/*  759 */         for (ExtInfo extInfo : extInfos)
/*  760 */           if ((extInfo.info1 != null) && 
/*  761 */             (extInfo.info1.trim().length() != 0))
/*      */           {
/*  764 */             List info = createInsertExtInfoList(companyId, 
/*  765 */               customerId, extInfo);
/*  766 */             baseParams.add(info.toArray(new Object[0]));
/*      */           }
/*  768 */         runner.batch(conn, "INSERT INTO T_Customer_Ext_Info (CompanyId,CustomerId,Type,Info1,Info2,Info3,Info4,CreateDate) VALUES (?,?,?,?,?,?,?,NOW())", 
/*  769 */           (Object[][])baseParams.toArray(new Object[0][]));
/*      */       }
/*      */ 
/*  773 */       addCustomerShareInfo(conn, runner, customerId, shareUserIds, 
/*  774 */         shareGroupIds, userId);
/*      */ 
/*  776 */       return customerId;
/*      */     } catch (SQLException e) {
/*  778 */       logger.warn("add company failed", e);
/*      */     } finally {
/*      */       try {
/*  781 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  783 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/*  787 */     return 0;
/*      */   }
/*      */ 
/*      */   public int addCustomer(int parentCustomerId, String name, int type, String userId, int companyId)
/*      */     throws DBException
/*      */   {
/*  803 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  804 */     if (conn == null)
/*  805 */       throw new DBException();
/*      */     try
/*      */     {
/*      */       int i;
/*  808 */       if (type == 0)
/*      */       {
/*  810 */         QueryRunner runner2 = new QueryRunner();
/*  811 */         Object[] result = (Object[])runner2.query(conn, "SELECT CustomerId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND Name = ? AND Type = 0 ", 
/*  812 */           new ArrayHandler(), new Object[] { Integer.valueOf(companyId), name });
/*  813 */         if (result != null) {
/*  814 */           int customerId = Util.str2Int(result[0]);
/*  815 */           return customerId * -1;
/*      */         }
/*      */       }
/*  818 */       GenKeyQueryRunner runner = new GenKeyQueryRunner(
/*  819 */         new ScalarHandler(), new String[] { "CustomerId" });
/*  820 */       runner.update(conn, "INSERT INTO T_Customer_Info (Name,Type,CompanyId,CreateUserId,UpdateUserId,CreateDate,UpdateDate,AssignDate,ParentCustomerId,Visible,PermissionsId,LastNoteId,DeleteFlag) VALUES (?,?,?,?,?,NOW(),NOW(),NOW(),?,?,?,-1,0)", new Object[] { name, Integer.valueOf(type), 
/*  821 */         Integer.valueOf(companyId), userId, userId, Integer.valueOf(parentCustomerId), 
/*  822 */         Integer.valueOf(4), userId });
/*  823 */       return ((Long)runner.getGeneratedKeys()).intValue();
/*      */     } catch (SQLException e) {
/*  825 */       logger.warn("add customer failed", e);
/*      */     } finally {
/*      */       try {
/*  828 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  830 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/*  834 */     return 0;
/*      */   }
/*      */ 
/*      */   public int updateImgPath(String userId, int customerId, String imgPath) {
/*  838 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  839 */     if (conn == null)
/*  840 */       return 0;
/*      */     try
/*      */     {
/*  843 */       QueryRunner runner = new QueryRunner();
/*  844 */       return runner.update(conn, "UPDATE T_Customer_Info SET ImgPath = ?, UpdateDate = NOW(), UpdateUserId = ? WHERE CustomerId = ?", new Object[] { 
/*  845 */         imgPath, userId, Integer.valueOf(customerId) });
/*      */     } catch (SQLException e) {
/*  847 */       logger.warn("update img path failed", e);
/*      */     } finally {
/*      */       try {
/*  850 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  852 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*  855 */     return 0;
/*      */   }
/*      */ 
/*      */   private void updateCustomerShareInfo(Connection conn, QueryRunner runner, int customerId, String shareUserIds, String shareGroupIds, String userId)
/*      */     throws SQLException
/*      */   {
/*  873 */     runner.update(conn, "DELETE FROM T_Customer_Permissions_Info WHERE CustomerId = ? ", Integer.valueOf(customerId));
/*      */ 
/*  875 */     addCustomerShareInfo(conn, runner, customerId, shareUserIds, 
/*  876 */       shareGroupIds, userId);
/*      */   }
/*      */ 
/*      */   private void addCustomerShareInfo(Connection conn, QueryRunner runner, int customerId, String shareUserIds, String shareGroupIds, String userId)
/*      */   {
/*  893 */     List params = new ArrayList();
/*  894 */     if ((shareUserIds != null) && (shareUserIds.length() > 0)) {
/*  895 */       for (String shareUserId : shareUserIds.split(","))
/*  896 */         if (shareUserId.trim().length() != 0)
/*      */         {
/*  899 */           if (!userId.equals(shareUserId.trim()))
/*      */           {
/*  903 */             List info = new ArrayList();
/*  904 */             info.add(Integer.valueOf(customerId));
/*  905 */             info.add(userId);
/*  906 */             info.add(shareUserId.trim());
/*  907 */             info.add(Integer.valueOf(1));
/*      */ 
/*  909 */             params.add(info.toArray(new Object[0]));
/*      */           }
/*      */         }
/*      */     }
/*  913 */     if ((shareGroupIds != null) && (shareGroupIds.length() > 0)) {
/*  914 */       for (String shareGroupId : shareGroupIds.split(",")) {
/*  915 */         if (shareGroupId.trim().length() != 0)
/*      */         {
/*  919 */           List info = new ArrayList();
/*  920 */           info.add(Integer.valueOf(customerId));
/*  921 */           info.add(userId);
/*  922 */           info.add(shareGroupId.trim());
/*  923 */           info.add(Integer.valueOf(2));
/*      */ 
/*  925 */           params.add(info.toArray(new Object[0]));
/*      */         }
/*      */       }
/*      */     }
/*  929 */     if (params.size() == 0) {
/*  930 */       return;
/*      */     }
/*      */ 
/*      */     try
/*      */     {
/*  935 */       runner.batch(conn, "INSERT IGNORE INTO T_Customer_Permissions_Info (CustomerId,OwnerUserId,ShareId,ShareType,CreateDate) VALUES (?,?,?,?,NOW())", 
/*  936 */         (Object[][])params.toArray(new Object[0][]));
/*      */     }
/*      */     catch (SQLException e) {
/*  939 */       logger.warn("add customer share info failed", e);
/*      */     }
/*      */   }
/*      */ 
/*      */   public int updateCompany(int customerId, String name, int categoryId, String note, int hasModifyRight, int companyVisible, String permissionsId, String oldPermissionsId, String shareUserIds, String oldShareUserIds, String shareGroupIds, String oldShareGroupIds, String tagIds, boolean hasTag, List<ExtInfo> extInfos, String deleteExtInfoIds, String userId, int companyId)
/*      */   {
/*  971 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  972 */     if (conn == null) {
/*  973 */       return 0;
/*      */     }
/*      */     try
/*      */     {
/*  977 */       QueryRunner runner = new QueryRunner();
/*  978 */       int count = 0;
/*      */ 
/*  982 */       if ((permissionsId != null) && 
/*  983 */         (!permissionsId.equals(oldPermissionsId)))
/*  984 */         count = runner.update(conn, 
/*  985 */           "UPDATE T_Customer_Info SET Name = ?, CategoryId = ?, Note = ?, Visible = ?, PermissionsId = ?, UpdateDate = NOW(), AssignDate = NOW(), UpdateUserId = ?, IndexFlag = 0 WHERE CustomerId = ?", new Object[] { name, 
/*  986 */           Integer.valueOf(categoryId), note, Integer.valueOf(companyVisible), permissionsId, 
/*  987 */           userId, Integer.valueOf(customerId) });
/*      */       else {
/*  989 */         count = runner.update(conn, "UPDATE T_Customer_Info SET Name = ?, CategoryId = ?, Note = ?, Visible = ?, PermissionsId = ?, UpdateDate = NOW(), UpdateUserId = ?, IndexFlag = 0 WHERE CustomerId = ?", new Object[] { name, 
/*  990 */           Integer.valueOf(categoryId), note, Integer.valueOf(companyVisible), permissionsId, 
/*  991 */           userId, Integer.valueOf(customerId) });
/*      */       }
/*      */ 
/*  995 */       if (hasModifyRight == 1) {
/*  996 */         if ((permissionsId != null) && 
/*  997 */           (!permissionsId.equals(oldPermissionsId)))
/*      */         {
/*  999 */           runner.update(conn, 
/* 1000 */             "UPDATE T_Customer_Info SET Visible = ?, PermissionsId = ?, UpdateUserId = ?, UpdateDate = NOW(), AssignDate = NOW(), IndexFlag = 0 WHERE ParentCustomerId = ?", new Object[] { 
/* 1001 */             Integer.valueOf(companyVisible), permissionsId, userId, Integer.valueOf(customerId) });
/*      */         }
/*      */ 
/* 1004 */         if (companyVisible == 1)
/*      */         {
/* 1006 */           if (((oldShareUserIds != null) && 
/* 1007 */             (oldShareUserIds.trim()
/* 1007 */             .length() > 0)) || (
/* 1008 */             (oldShareGroupIds != null) && 
/* 1009 */             (oldShareGroupIds
/* 1009 */             .trim().length() > 0))) {
/* 1010 */             runner.update(conn, "DELETE FROM T_Customer_Permissions_Info WHERE CustomerId = ? ", 
/* 1011 */               Integer.valueOf(customerId));
/*      */           }
/*      */         }
/*      */ 
/* 1015 */         if (shareUserIds == null) {
/* 1016 */           shareUserIds = "";
/*      */         }
/* 1018 */         if (oldShareUserIds == null) {
/* 1019 */           oldShareUserIds = "";
/*      */         }
/*      */ 
/* 1022 */         if (shareGroupIds == null) {
/* 1023 */           shareGroupIds = "";
/*      */         }
/* 1025 */         if (oldShareGroupIds == null) {
/* 1026 */           oldShareGroupIds = "";
/*      */         }
/* 1028 */         if ((!shareUserIds.equals(oldShareUserIds)) || 
/* 1029 */           (!shareGroupIds.equals(oldShareGroupIds)))
/*      */         {
/* 1031 */           updateCustomerShareInfo(conn, runner, customerId, 
/* 1032 */             shareUserIds, shareGroupIds, userId);
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/* 1037 */       if (hasTag)
/*      */       {
/* 1039 */         runner.update(conn, "DELETE FROM T_Customer_Tag_Info WHERE CustomerId = ?", Integer.valueOf(customerId));
/* 1040 */         if ((tagIds != null) && (tagIds.trim().length() > 0)) {
/* 1041 */           List baseParams = new ArrayList();
/* 1042 */           for (String id : tagIds.split(",")) {
/* 1043 */             List info = new ArrayList();
/* 1044 */             int tagId = Util.str2Int(id);
/* 1045 */             if (tagId > 0)
/*      */             {
/* 1048 */               info.add(Integer.valueOf(customerId));
/* 1049 */               info.add(Integer.valueOf(tagId));
/* 1050 */               info.add(Integer.valueOf(companyId));
/* 1051 */               info.add(userId);
/* 1052 */               baseParams.add(info.toArray(new Object[0]));
/*      */             }
/*      */           }
/* 1054 */           runner.batch(conn, "INSERT INTO T_Customer_Tag_Info (CustomerId,TagId,CompanyId,CreateDate,CreateUserId) VALUES (?,?,?,NOW(),?)", 
/* 1055 */             (Object[][])baseParams.toArray(new Object[0][]));
/*      */         }
/*      */       }
/*      */ 
/* 1059 */       if ((extInfos != null) && (extInfos.size() > 0)) {
/* 1060 */         List addParams = new ArrayList();
/* 1061 */         List updateParams = new ArrayList();
/* 1062 */         Object deleteParams = new ArrayList();
/* 1063 */         for (Iterator iter = extInfos.iterator(); iter.hasNext(); ) { ExtInfo extInfo = (ExtInfo)iter.next();
/* 1064 */           List info = new ArrayList();
/* 1065 */           if (extInfo.id > 0) {
/* 1066 */             if ((extInfo.info1 == null) || 
/* 1067 */               (extInfo.info1.trim().length() == 0)) {
/* 1068 */               info.add(Integer.valueOf(extInfo.id));
/* 1069 */               ((List)deleteParams).add(info.toArray(new Object[0]));
/*      */             } else {
/* 1071 */               info.add(extInfo.info1);
/* 1072 */               info.add(extInfo.info2);
/* 1073 */               info.add(extInfo.info3);
/* 1074 */               info.add(extInfo.info4);
/* 1075 */               info.add(Integer.valueOf(extInfo.id));
/* 1076 */               updateParams.add(info.toArray(new Object[0]));
/*      */             }
/*      */           }
/* 1079 */           else if ((extInfo.info1 != null) && 
/* 1080 */             (extInfo.info1.trim().length() != 0))
/*      */           {
/* 1084 */             info.add(Integer.valueOf(companyId));
/* 1085 */             info.add(Integer.valueOf(customerId));
/* 1086 */             info.add(Integer.valueOf(extInfo.type));
/* 1087 */             info.add(extInfo.info1);
/* 1088 */             info.add(extInfo.info2);
/* 1089 */             info.add(extInfo.info3);
/* 1090 */             info.add(extInfo.info4);
/* 1091 */             addParams.add(info.toArray(new Object[0]));
/*      */           }
/*      */         }
/*      */ 
/* 1095 */         if (updateParams.size() > 0) {
/* 1096 */           runner.batch(conn, "UPDATE T_Customer_Ext_Info SET Info1 = ?, Info2 = ?, Info3 = ?, Info4 = ? WHERE CustomerExtId = ?", 
/* 1097 */             (Object[][])updateParams.toArray(new Object[0][]));
/*      */         }
/*      */ 
/* 1100 */         if (addParams.size() > 0) {
/* 1101 */           runner.batch(conn, "INSERT INTO T_Customer_Ext_Info (CompanyId,CustomerId,Type,Info1,Info2,Info3,Info4,CreateDate) VALUES (?,?,?,?,?,?,?,NOW())", 
/* 1102 */             (Object[][])addParams.toArray(new Object[0][]));
/*      */         }
/*      */ 
/* 1105 */         if (((List)deleteParams).size() > 0) {
/* 1106 */           runner.batch(conn, "DELETE FROM T_Customer_Ext_Info WHERE CustomerExtId = ?", 
/* 1107 */             (Object[][])((List)deleteParams).toArray(new Object[0][]));
/*      */         }
/*      */       }
/*      */ 
/* 1111 */       if ((deleteExtInfoIds != null) && 
/* 1112 */         (deleteExtInfoIds.trim().length() > 0)) {
/* 1113 */         String deleteSQL = "DELETE FROM T_Customer_Ext_Info WHERE CustomerExtId IN (" + 
/* 1114 */           deleteExtInfoIds + ")";
/* 1115 */         runner.update(conn, deleteSQL);
/*      */       }
/* 1117 */       return count;
/*      */     } catch (SQLException e) {
/* 1119 */       logger.warn("update company failed", e);
/*      */     } finally {
/*      */       try {
/* 1122 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 1124 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/* 1128 */     return 0;
/*      */   }
/*      */ 
/*      */   private String[] getShareIds(Connection conn, int customerId)
/*      */   {
/* 1138 */     if (customerId <= 0) {
/* 1139 */       return null;
/*      */     }
/*      */ 
/* 1142 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 1144 */       List result = (List)runner.query(conn, 
/* 1145 */         "SELECT ShareId, ShareType FROM T_Customer_Permissions_Info WHERE CustomerId = ? ", new MyMapListHandler(), new Object[] { 
/* 1146 */         Integer.valueOf(customerId) });
/* 1147 */       if ((result == null) || (result.size() == 0) || (result.get(0) == null)) {
/* 1148 */         return null;
/*      */       }
/*      */ 
/* 1151 */       StringBuffer userIds = new StringBuffer();
/* 1152 */       StringBuffer groupIds = new StringBuffer();
/* 1153 */       for (Object item : result) {
/* 1154 */         String id = (String)((Map)item).get("ShareId");
/* 1155 */         if ((id != null) && (id.trim().length() != 0))
/*      */         {
/* 1158 */           int type = ((Integer)((Map)item).get("ShareType")).intValue();
/*      */ 
/* 1160 */           if (type == 1) {
/* 1161 */             if (userIds.length() > 0) {
/* 1162 */               userIds.append(",");
/*      */             }
/* 1164 */             userIds.append(id);
/*      */           }
/*      */           else
/*      */           {
/* 1168 */             if (groupIds.length() > 0) {
/* 1169 */               groupIds.append(",");
/*      */             }
/* 1171 */             groupIds.append(id);
/*      */           }
/*      */         }
/*      */       }
/* 1174 */       return new String[] { userIds.toString(), groupIds.toString() };
/*      */     }
/*      */     catch (SQLException e) {
/* 1177 */       logger.warn("get customer failed", e);
/*      */     }
/*      */ 
/* 1180 */     return null;
/*      */   }
/*      */ 
/*      */   public CustomerInfo getCustomerInfo(int id, boolean hasTags, boolean hasExtInfo)
/*      */     throws DBException
/*      */   {
/* 1193 */     if (id <= 0) {
/* 1194 */       return null;
/*      */     }
/*      */ 
/* 1197 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 1198 */     if (conn == null) {
/* 1199 */       throw new DBException();
/*      */     }
/*      */ 
/* 1202 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 1204 */       Map result = (Map)runner.query(conn, 
/* 1205 */         "SELECT A.Name, A.CategoryId, A.Note, A.Visible, A.PermissionsId, A.ImgPath, A.CreateUserId, A.AssignDate, A.CreateDate, B.Name AS ParentCustomerName, A.ParentCustomerId, A.Title, A.Type, B.Visible AS ParentVisible, B.PermissionsId AS ParentPermissionsId, B.DeleteFlag AS ParentDeleteFlag FROM T_Customer_Info AS A LEFT JOIN T_Customer_Info AS B ON A.ParentCustomerId = B.CustomerId WHERE A.DeleteFlag = 0 AND A.CustomerId = ?", new MyMapHandler(), new Object[] { Integer.valueOf(id) });
/* 1206 */       if ((result == null) || (result.size() == 0)) {
/* 1207 */         return null;
/*      */       }
/*      */ 
/* 1210 */       int parentDeleteFlag = Util.str2Int(result.get("ParentDeleteFlag"));
/* 1211 */       if (parentDeleteFlag == 1) {
/* 1212 */         return null;
/*      */       }
/*      */ 
/* 1215 */       CustomerInfo customer = new CustomerInfo();
/* 1216 */       customer.type = Util.str2Int(result.get("Type"));
/* 1217 */       customer.id = id;
/* 1218 */       customer.categoryId = Util.str2Int(result.get("CategoryId"));
/* 1219 */       customer.name = ((String)result.get("Name"));
/* 1220 */       customer.parentName = ((String)result.get("ParentCustomerName"));
/* 1221 */       customer.title = ((String)result.get("Title"));
/* 1222 */       customer.parentId = Util.str2Int(result.get("ParentCustomerId"));
/* 1223 */       customer.visible = Util.str2Int(result.get("Visible"));
/* 1224 */       customer.permissionsId = ((String)result.get("PermissionsId"));
/*      */ 
/* 1227 */       if (customer.visible == 4) {
/* 1228 */         if (customer.isCompany()) {
/* 1229 */           getShareInfo(conn, customer, customer.id);
/*      */         }
/* 1232 */         else if (customer.parentId == -1)
/*      */         {
/* 1234 */           getShareInfo(conn, customer, customer.id);
/*      */         }
/* 1236 */         else getShareInfo(conn, customer, customer.parentId);
/*      */ 
/*      */       }
/*      */ 
/* 1241 */       if (!customer.isCompany()) {
/* 1242 */         customer.parentVisible = Util.str2Int(result
/* 1243 */           .get("ParentVisible"));
/* 1244 */         customer.parentPermissionsId = 
/* 1245 */           ((String)result
/* 1245 */           .get("ParentPermissionsId"));
/*      */ 
/* 1248 */         if ((customer.parentId > 0) && (
/* 1249 */           (customer.parentVisible != customer.visible) || 
/* 1251 */           (!customer.parentPermissionsId
/* 1251 */           .equals(customer.permissionsId)))) {
/* 1252 */           runner.update(conn, 
/* 1253 */             "UPDATE T_Customer_Info SET Visible = ?, PermissionsId = ? WHERE CustomerId = ?", new Object[] { 
/* 1254 */             Integer.valueOf(customer.parentVisible), 
/* 1255 */             customer.parentPermissionsId, Integer.valueOf(customer.id) });
/* 1256 */           customer.visible = customer.parentVisible;
/* 1257 */           customer.permissionsId = customer.parentPermissionsId;
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/* 1262 */       customer.createUserId = ((String)result.get("CreateUserId"));
/* 1263 */       customer.note = ((String)result.get("Note"));
/* 1264 */       customer.imgPath = ((String)result.get("ImgPath"));
/* 1265 */       customer.assignDate = ((String)result.get("AssignDate"));
/* 1266 */       customer.createDate = ((String)result.get("CreateDate"));
/*      */ 
/* 1268 */       if ((customer.imgPath == null) || (customer.imgPath.length() == 0)) {
/* 1269 */         if (customer.type == 0)
/* 1270 */           customer.imgPath = "images/avatar_company.png";
/*      */         else {
/* 1272 */           customer.imgPath = "images/avatar_person.png";
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/* 1277 */       if (hasExtInfo) {
/* 1278 */         List result2 = (List)runner.query(conn, 
/* 1279 */           "SELECT * FROM T_Customer_Ext_Info WHERE CustomerId = ? ORDER BY Type ASC, Info3 ASC, CreateDate ASC", new MyMapListHandler(), new Object[] { Integer.valueOf(id) });
/*      */ 
/* 1281 */         if (result2 != null) {
/* 1282 */           for (Object extInfoO : result2) {
                        Map extInfo = (Map)(extInfoO);
/* 1283 */             ExtInfo info = new ExtInfo();
/* 1284 */             info.id = Util.str2Int(extInfo.get("CustomerExtId"));
/* 1285 */             info.type = Util.str2Int(extInfo.get("Type"));
/* 1286 */             info.info1 = ((String)extInfo.get("Info1"));
/* 1287 */             info.info2 = ((String)extInfo.get("Info2"));
/* 1288 */             info.info3 = ((String)extInfo.get("Info3"));
/* 1289 */             info.info4 = ((String)extInfo.get("Info4"));
/* 1290 */             customer.addExtInfo(info);
/*      */           }
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/* 1296 */       if (hasTags) {
/* 1297 */         List result2 = (List)runner.query(conn, 
/* 1298 */           "SELECT B.TagId, B.TagName FROM T_Customer_Tag_Info AS A INNER JOIN T_Tag_Info AS B ON A.TagId = B.TagId WHERE A.CustomerId = ? ORDER BY A.CreateDate ASC", new MyMapListHandler(), new Object[] { Integer.valueOf(id) });
/* 1299 */         if (result2 != null) {
/* 1300 */           for (Object tagInfoO : result2) {
                       Map tagInfo = (Map)tagInfoO;
/* 1301 */             Tag tag = new Tag();
/* 1302 */             tag.id = Util.str2Int(tagInfo.get("TagId"));
/* 1303 */             tag.name = ((String)tagInfo.get("TagName"));
/* 1304 */             customer.addTag(tag);
/*      */           }
/*      */         }
/*      */       }
/* 1308 */       return customer;
/*      */     } catch (SQLException e) {
/* 1310 */       logger.warn("get customer failed", e);
/*      */     } finally {
/*      */       try {
/* 1313 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 1315 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 1318 */     return null;
/*      */   }
/*      */ 
/*      */   private void getShareInfo(Connection conn, CustomerInfo customer, int customerId)
/*      */   {
/* 1331 */     String[] ids = getShareIds(conn, customerId);
/* 1332 */     if ((ids == null) || (ids.length != 2)) {
/* 1333 */       return;
/*      */     }
/*      */ 
/* 1336 */     customer.shareUserIds = ids[0];
/* 1337 */     customer.shareGroupIds = ids[1];
/*      */   }
/*      */ 
/*      */   public CustomerInfo getCustomerMiniInfo(int id)
/*      */     throws DBException
/*      */   {
/* 1349 */     if (id <= 0) {
/* 1350 */       return null;
/*      */     }
/*      */ 
/* 1353 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 1354 */     if (conn == null) {
/* 1355 */       throw new DBException();
/*      */     }
/*      */ 
/* 1358 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 1360 */       Map result = (Map)runner.query(conn, 
/* 1361 */         "SELECT A.Name, A.Type, A.Visible, A.PermissionsId, B.Visible AS ParentVisible, B.PermissionsId AS ParentPermissionsId FROM T_Customer_Info AS A LEFT JOIN T_Customer_Info AS B ON A.ParentCustomerId = B.CustomerId WHERE A.DeleteFlag = 0 AND A.CustomerId = ?", new MyMapHandler(), new Object[] { Integer.valueOf(id) });
/* 1362 */       if ((result == null) || (result.size() == 0)) {
/* 1363 */         return null;
/*      */       }
/* 1365 */       CustomerInfo customer = new CustomerInfo();
/* 1366 */       customer.name = ((String)result.get("Name"));
/* 1367 */       customer.type = Util.str2Int(result.get("Type"));
/* 1368 */       customer.id = id;
/* 1369 */       customer.visible = Util.str2Int(result.get("Visible"));
/* 1370 */       customer.permissionsId = ((String)result.get("PermissionsId"));
/*      */ 
/* 1372 */       if (!customer.isCompany()) {
/* 1373 */         customer.parentVisible = Util.str2Int(result
/* 1374 */           .get("ParentVisible"));
/* 1375 */         customer.parentPermissionsId = 
/* 1376 */           ((String)result
/* 1376 */           .get("ParentPermissionsId"));
/*      */       }
/*      */ 
/* 1379 */       return customer;
/*      */     } catch (SQLException e) {
/* 1381 */       logger.warn("get customer failed", e);
/*      */     } finally {
/*      */       try {
/* 1384 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 1386 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 1389 */     return null;
/*      */   }
/*      */ 
/*      */   public String getName(int id)
/*      */   {
/* 1400 */     if (id <= 0) {
/* 1401 */       return null;
/*      */     }
/*      */ 
/* 1404 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 1405 */     if (conn == null) {
/* 1406 */       return null;
/*      */     }
/*      */ 
/* 1409 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 1412 */       Object result = runner.query(conn, "SELECT Name FROM T_Customer_Info WHERE CustomerId = ?", 
/* 1413 */         new ScalarHandler(1), new Object[] { Integer.valueOf(id) });
/* 1414 */       if (result == null) {
/* 1415 */         return null;
/*      */       }
/* 1417 */       return (String)result;
/*      */     } catch (SQLException e) {
/* 1419 */       logger.warn("get customer failed", e);
/*      */     } finally {
/*      */       try {
/* 1422 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 1424 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 1427 */     return null;
/*      */   }
/*      */ 
/*      */   public int addPeople(String companyName, String name, String title, int categoryId, String note, int visible, String permissionsId, String shareUserIds, String shareGroupIds, String tagIds, List<ExtInfo> extInfos, String userId, int companyId)
/*      */   {
/* 1452 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 1453 */     if (conn == null) {
/* 1454 */       return 0;
/*      */     }
/* 1456 */     int belongCustomerId = -1;
/*      */     try
/*      */     {
/* 1460 */       if ((companyName != null) && (companyName.trim().length() > 0))
/*      */       {
/* 1463 */         QueryRunner runner2 = new QueryRunner();
/* 1464 */         List result = (List)runner2.query(conn, 
/* 1465 */           "SELECT CustomerId, Visible, PermissionsId, CategoryId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND Name = ? AND Type = 0 ", new MyMapListHandler(), new Object[] { 
/* 1466 */           Integer.valueOf(companyId), companyName });
/*      */ 
/* 1468 */         if ((result == null) || (result.size() == 0)) {
/* 1469 */           belongCustomerId = -1;
/*      */         } else {
/* 1471 */           belongCustomerId = Util.str2Int(((Map)result.get(0)).get(
/* 1472 */             "CustomerId"));
/* 1473 */           visible = Util.str2Int(((Map)result.get(0)).get("Visible"));
/* 1474 */           permissionsId = 
/* 1475 */             (String)((Map)result.get(0))
/* 1475 */             .get("PermissionsId");
/* 1476 */           if (categoryId == -1) {
/* 1477 */             categoryId = Util.str2Int(((Map)result.get(0)).get(
/* 1478 */               "CategoryId"));
/*      */           }
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/* 1484 */       GenKeyQueryRunner runner = new GenKeyQueryRunner(
/* 1485 */         new ScalarHandler(), new String[] { "CustomerId" });
/*      */ 
/* 1488 */       if ((belongCustomerId == -1) && (companyName != null) && 
/* 1489 */         (companyName.trim().length() > 0))
/*      */       {
/* 1494 */         runner.update(conn, "INSERT INTO T_Customer_Info (Name,Type,CategoryId,Title,Note,Visible,PermissionsId,CompanyId,CreateUserId,UpdateUserId,CreateDate,UpdateDate,AssignDate,ParentCustomerId,ImportId,LastNoteId,DeleteFlag) VALUES (?,?,?,?,?,?,?,?,?,?,NOW(),NOW(),NOW(),?,?,-1,0)", new Object[] { companyName, 
/* 1495 */           Integer.valueOf(0), Integer.valueOf(categoryId), "", "", Integer.valueOf(visible), 
/* 1496 */           permissionsId, Integer.valueOf(companyId), userId, userId, Integer.valueOf(-1), Integer.valueOf(0) });
/* 1497 */         belongCustomerId = ((Long)runner.getGeneratedKeys())
/* 1498 */           .intValue();
/*      */ 
/* 1501 */         if (belongCustomerId > 0) {
/* 1502 */           addCustomerShareInfo(conn, runner, belongCustomerId, 
/* 1503 */             shareUserIds, shareGroupIds, userId);
/*      */         }
/*      */       }
/*      */ 
/* 1507 */       runner.update(conn, "INSERT INTO T_Customer_Info (Name,Type,CategoryId,Title,Note,Visible,PermissionsId,CompanyId,CreateUserId,UpdateUserId,CreateDate,UpdateDate,AssignDate,ParentCustomerId,ImportId,LastNoteId,DeleteFlag) VALUES (?,?,?,?,?,?,?,?,?,?,NOW(),NOW(),NOW(),?,?,-1,0)", new Object[] { name, 
/* 1508 */         Integer.valueOf(1), Integer.valueOf(categoryId), title, note, Integer.valueOf(visible), 
/* 1509 */         permissionsId, Integer.valueOf(companyId), userId, userId, Integer.valueOf(belongCustomerId), 
/* 1510 */         Integer.valueOf(0) });
/*      */ 
/* 1512 */       int customerId = ((Long)runner.getGeneratedKeys()).intValue();
/*      */       int k;
/* 1514 */       if (customerId <= 0) {
/* 1515 */         return customerId;
/*      */       }
/*      */ 
/* 1519 */       if ((tagIds != null) && (tagIds.trim().length() > 0)) {
/* 1520 */         List baseParams = new ArrayList();
/* 1521 */         for (String id : tagIds.split(",")) {
/* 1522 */           List info = new ArrayList();
/* 1523 */           int tagId = Util.str2Int(id);
/* 1524 */           if (tagId > 0)
/*      */           {
/* 1527 */             info.add(Integer.valueOf(customerId));
/* 1528 */             info.add(Integer.valueOf(tagId));
/* 1529 */             info.add(Integer.valueOf(companyId));
/* 1530 */             info.add(userId);
/* 1531 */             baseParams.add(info.toArray(new Object[0]));
/*      */           }
/*      */         }
/* 1533 */         runner.batch(conn, "INSERT INTO T_Customer_Tag_Info (CustomerId,TagId,CompanyId,CreateDate,CreateUserId) VALUES (?,?,?,NOW(),?)", 
/* 1534 */           (Object[][])baseParams.toArray(new Object[0][]));
/*      */       }
/*      */ 
/* 1538 */       if ((extInfos != null) && (extInfos.size() > 0)) {
/* 1539 */         List baseParams = new ArrayList();
/* 1540 */         for (ExtInfo extInfo : extInfos)
/* 1541 */           if ((extInfo.info1 != null) && 
/* 1542 */             (extInfo.info1.trim().length() != 0))
/*      */           {
/* 1545 */             List info = createInsertExtInfoList(companyId, 
/* 1546 */               customerId, extInfo);
/* 1547 */             baseParams.add(info.toArray(new Object[0]));
/*      */           }
/* 1549 */         runner.batch(conn, "INSERT INTO T_Customer_Ext_Info (CompanyId,CustomerId,Type,Info1,Info2,Info3,Info4,CreateDate) VALUES (?,?,?,?,?,?,?,NOW())", 
/* 1550 */           (Object[][])baseParams.toArray(new Object[0][]));
/*      */       }
/*      */ 
/* 1554 */       if (belongCustomerId == -1) {
/* 1555 */         addCustomerShareInfo(conn, runner, customerId, 
/* 1556 */           shareUserIds, shareGroupIds, userId);
/*      */       }
/*      */ 
/* 1560 */       return customerId;
/*      */     }
/*      */     catch (SQLException e)
/*      */     {
/* 1567 */       logger.warn("add people", e);
/*      */     } finally {
/*      */       try {
/* 1570 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 1572 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 1575 */     return 0;
/*      */   }
/*      */ 
/*      */   public int addPeople(int belongCustomerId, String name, String title, int categoryId, String note, int visible, String permissionsId, String shareUserIds, String shareGroupIds, String tagIds, List<ExtInfo> extInfos, String userId, int companyId)
/*      */   {
/* 1600 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 1601 */     if (conn == null) {
/* 1602 */       return 0;
/*      */     }
/*      */ 
/*      */     try
/*      */     {
/* 1607 */       QueryRunner runner2 = new QueryRunner();
/* 1608 */       Object[] result = (Object[])runner2.query(conn, 
/* 1609 */         "SELECT CustomerId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND ParentCustomerId = ? AND Name = ? AND Type = 1 ", new ArrayHandler(), new Object[] { 
/* 1610 */         Integer.valueOf(companyId), Integer.valueOf(belongCustomerId), name });
/*      */       int k;
/* 1611 */       if (result != null) {
/* 1612 */         int customerId = Util.str2Int(result[0]);
/* 1613 */         return customerId * -1;
/*      */       }
/*      */ 
/* 1617 */       GenKeyQueryRunner runner = new GenKeyQueryRunner(
/* 1618 */         new ScalarHandler(), new String[] { "CustomerId" });
/*      */ 
/* 1620 */       runner.update(conn, "INSERT INTO T_Customer_Info (Name,Type,CategoryId,Title,Note,Visible,PermissionsId,CompanyId,CreateUserId,UpdateUserId,CreateDate,UpdateDate,AssignDate,ParentCustomerId,ImportId,LastNoteId,DeleteFlag) VALUES (?,?,?,?,?,?,?,?,?,?,NOW(),NOW(),NOW(),?,?,-1,0)", new Object[] { name, 
/* 1621 */         Integer.valueOf(1), Integer.valueOf(categoryId), title, note, Integer.valueOf(visible), 
/* 1622 */         permissionsId, Integer.valueOf(companyId), userId, userId, Integer.valueOf(belongCustomerId), 
/* 1623 */         Integer.valueOf(0) });
/*      */ 
/* 1625 */       int customerId = ((Long)runner.getGeneratedKeys()).intValue();
/*      */ 
/* 1627 */       if (customerId <= 0) {
/* 1628 */         return customerId;
/*      */       }
/*      */ 
/* 1632 */       if ((tagIds != null) && (tagIds.trim().length() > 0)) {
/* 1633 */         List baseParams = new ArrayList();
/* 1634 */         for (String id : tagIds.split(",")) {
/* 1635 */           List info = new ArrayList();
/* 1636 */           int tagId = Util.str2Int(id);
/* 1637 */           if (tagId > 0)
/*      */           {
/* 1640 */             info.add(Integer.valueOf(customerId));
/* 1641 */             info.add(Integer.valueOf(tagId));
/* 1642 */             info.add(Integer.valueOf(companyId));
/* 1643 */             info.add(userId);
/* 1644 */             baseParams.add(info.toArray(new Object[0]));
/*      */           }
/*      */         }
/* 1646 */         runner.batch(conn, "INSERT INTO T_Customer_Tag_Info (CustomerId,TagId,CompanyId,CreateDate,CreateUserId) VALUES (?,?,?,NOW(),?)", 
/* 1647 */           (Object[][])baseParams.toArray(new Object[0][]));
/*      */       }
/*      */ 
/* 1651 */       if ((extInfos != null) && (extInfos.size() > 0)) {
/* 1652 */         List baseParams = new ArrayList();
/* 1653 */         for (ExtInfo extInfo : extInfos)
/* 1654 */           if ((extInfo.info1 != null) && 
/* 1655 */             (extInfo.info1.trim().length() != 0))
/*      */           {
/* 1658 */             List info = createInsertExtInfoList(companyId, 
/* 1659 */               customerId, extInfo);
/* 1660 */             baseParams.add(info.toArray(new Object[0]));
/*      */           }
/* 1662 */         runner.batch(conn, "INSERT INTO T_Customer_Ext_Info (CompanyId,CustomerId,Type,Info1,Info2,Info3,Info4,CreateDate) VALUES (?,?,?,?,?,?,?,NOW())", 
/* 1663 */           (Object[][])baseParams.toArray(new Object[0][]));
/*      */       }
/*      */ 
/* 1667 */       if (belongCustomerId == -1) {
/* 1668 */         addCustomerShareInfo(conn, runner, customerId, 
/* 1669 */           shareUserIds, shareGroupIds, userId);
/*      */       }
/*      */ 
/* 1673 */       return customerId;
/*      */     }
/*      */     catch (SQLException e)
/*      */     {
/* 1680 */       logger.warn("add people", e);
/*      */     } finally {
/*      */       try {
/* 1683 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 1685 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 1688 */     return 0;
/*      */   }
/*      */ 
/*      */   public int updatePeople(int oldBelongCustomerId, String companyName, int customerId, String name, String title, int categoryId, String note, int hasModifyRight, int visible, String permissionsId, String oldPermissionsId, String shareUserIds, String oldShareUserIds, String shareGroupIds, String oldShareGroupIds, String tagIds, boolean hasTag, List<ExtInfo> extInfos, String deleteExtInfoIds, String userId, int companyId)
/*      */   {
/* 1722 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 1723 */     if (conn == null) {
/* 1724 */       return 0;
/*      */     }
/*      */ 
/* 1727 */     int belongCustomerId = -1;
/*      */     try {
/* 1729 */       QueryRunner runner = new QueryRunner();
/* 1730 */       if ((oldBelongCustomerId != belongCustomerId) && 
/* 1731 */         (companyName != null) && (companyName.trim().length() > 0))
/*      */       {
/* 1733 */         List result = (List)runner.query(conn, 
/* 1734 */           "SELECT CustomerId, Visible, PermissionsId, CategoryId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND Name = ? AND Type = 0 ", new MyMapListHandler(), new Object[] { 
/* 1735 */           Integer.valueOf(companyId), companyName });
/* 1736 */         if ((result == null) || (result.size() == 0)) {
/* 1737 */           belongCustomerId = -1;
/*      */         } else {
/* 1739 */           belongCustomerId = Util.str2Int(((Map)result.get(0)).get(
/* 1740 */             "CustomerId"));
/* 1741 */           visible = Util.str2Int(((Map)result.get(0)).get("Visible"));
/* 1742 */           permissionsId = 
/* 1743 */             (String)((Map)result.get(0))
/* 1743 */             .get("PermissionsId");
/* 1744 */           if (categoryId == -1) {
/* 1745 */             categoryId = Util.str2Int(((Map)result.get(0)).get(
/* 1746 */               "CategoryId"));
/*      */           }
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/* 1752 */       if ((belongCustomerId == -1) && (companyName != null) && 
/* 1753 */         (companyName.trim().length() > 0))
/*      */       {
/* 1755 */         GenKeyQueryRunner runner2 = new GenKeyQueryRunner(
/* 1756 */           new ScalarHandler(), new String[] { "CustomerId" });
/* 1757 */         runner2.update(conn, "INSERT INTO T_Customer_Info (Name,Type,CategoryId,Title,Note,Visible,PermissionsId,CompanyId,CreateUserId,UpdateUserId,CreateDate,UpdateDate,AssignDate,ParentCustomerId,ImportId,LastNoteId,DeleteFlag) VALUES (?,?,?,?,?,?,?,?,?,?,NOW(),NOW(),NOW(),?,?,-1,0)", new Object[] { companyName, 
/* 1758 */           Integer.valueOf(0), Integer.valueOf(categoryId), "", "", Integer.valueOf(visible), 
/* 1759 */           permissionsId, Integer.valueOf(companyId), userId, userId, Integer.valueOf(-1), Integer.valueOf(0) });
/* 1760 */         belongCustomerId = ((Long)runner2.getGeneratedKeys())
/* 1761 */           .intValue();
/*      */       }
/* 1763 */       int count = 0;
/* 1764 */       if (categoryId >= 0)
/*      */       {
/* 1766 */         count = runner.update(conn, 
/* 1767 */           "UPDATE T_Customer_Info SET Name = ?, Title = ?, Note = ?, Visible = ?, PermissionsId = ?, ParentCustomerId = ?, CategoryId = ?, UpdateDate = NOW(), UpdateUserId = ?, IndexFlag = 0 WHERE CustomerId = ?", new Object[] { name, 
/* 1768 */           title, note, Integer.valueOf(visible), permissionsId, Integer.valueOf(belongCustomerId), 
/* 1769 */           Integer.valueOf(categoryId), userId, Integer.valueOf(customerId) });
/*      */       }
/*      */       else
/*      */       {
/* 1773 */         count = runner.update(conn, "UPDATE T_Customer_Info SET Name = ?, Title = ?, Note = ?, Visible = ?, PermissionsId = ?, ParentCustomerId = ?, UpdateDate = NOW(), UpdateUserId = ?, IndexFlag = 0 WHERE CustomerId = ?", new Object[] { 
/* 1774 */           name, title, note, Integer.valueOf(visible), permissionsId, 
/* 1775 */           Integer.valueOf(belongCustomerId), userId, Integer.valueOf(customerId) });
/*      */       }
/*      */ 
/* 1778 */       if (hasModifyRight == 1)
/*      */       {
/* 1780 */         if (visible == 1)
/*      */         {
/* 1782 */           if (((oldShareUserIds != null) && 
/* 1783 */             (oldShareUserIds.trim()
/* 1783 */             .length() > 0)) || (
/* 1784 */             (oldShareGroupIds != null) && 
/* 1785 */             (oldShareGroupIds
/* 1785 */             .trim().length() > 0))) {
/* 1786 */             runner.update(conn, "DELETE FROM T_Customer_Permissions_Info WHERE CustomerId = ? ", 
/* 1787 */               Integer.valueOf(customerId));
/*      */           }
/*      */         }
/*      */ 
/* 1791 */         if (shareUserIds == null) {
/* 1792 */           shareUserIds = "";
/*      */         }
/* 1794 */         if (oldShareUserIds == null) {
/* 1795 */           oldShareUserIds = "";
/*      */         }
/*      */ 
/* 1798 */         if (shareGroupIds == null) {
/* 1799 */           shareGroupIds = "";
/*      */         }
/* 1801 */         if (oldShareGroupIds == null) {
/* 1802 */           oldShareGroupIds = "";
/*      */         }
/*      */ 
/* 1805 */         if ((!shareUserIds.equals(oldShareUserIds)) || 
/* 1806 */           (!shareGroupIds.equals(oldShareGroupIds)))
/*      */         {
/* 1808 */           updateCustomerShareInfo(conn, runner, customerId, 
/* 1809 */             shareUserIds, shareGroupIds, userId);
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/* 1814 */       if (hasTag)
/*      */       {
/* 1816 */         runner.update(conn, "DELETE FROM T_Customer_Tag_Info WHERE CustomerId = ?", Integer.valueOf(customerId));
/* 1817 */         if ((tagIds != null) && (tagIds.trim().length() > 0)) {
/* 1818 */           List baseParams = new ArrayList();
/* 1819 */           for (String id : tagIds.split(",")) {
/* 1820 */             List info = new ArrayList();
/* 1821 */             int tagId = Util.str2Int(id);
/* 1822 */             if (tagId > 0)
/*      */             {
/* 1825 */               info.add(Integer.valueOf(customerId));
/* 1826 */               info.add(Integer.valueOf(tagId));
/* 1827 */               info.add(Integer.valueOf(companyId));
/* 1828 */               info.add(userId);
/* 1829 */               baseParams.add(info.toArray(new Object[0]));
/*      */             }
/*      */           }
/* 1831 */           runner.batch(conn, "INSERT INTO T_Customer_Tag_Info (CustomerId,TagId,CompanyId,CreateDate,CreateUserId) VALUES (?,?,?,NOW(),?)", 
/* 1832 */             (Object[][])baseParams.toArray(new Object[0][]));
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/* 1837 */       if ((extInfos != null) && (extInfos.size() > 0)) {
/* 1838 */         List addParams = new ArrayList();
/* 1839 */         List updateParams = new ArrayList();
/* 1840 */         Object deleteParams = new ArrayList();
/* 1841 */         for (Iterator itea = extInfos.iterator(); itea.hasNext(); ) { ExtInfo extInfo = (ExtInfo)itea.next();
/* 1842 */           List info = new ArrayList();
/* 1843 */           if (extInfo.id > 0) {
/* 1844 */             if ((extInfo.info1 == null) || 
/* 1845 */               (extInfo.info1.trim().length() == 0)) {
/* 1846 */               info.add(Integer.valueOf(extInfo.id));
/* 1847 */               ((List)deleteParams).add(info.toArray(new Object[0]));
/*      */             } else {
/* 1849 */               info.add(extInfo.info1);
/* 1850 */               info.add(extInfo.info2);
/* 1851 */               info.add(extInfo.info3);
/* 1852 */               info.add(extInfo.info4);
/* 1853 */               info.add(Integer.valueOf(extInfo.id));
/* 1854 */               updateParams.add(info.toArray(new Object[0]));
/*      */             }
/*      */           } else {
/* 1857 */             info.add(Integer.valueOf(companyId));
/* 1858 */             info.add(Integer.valueOf(customerId));
/* 1859 */             info.add(Integer.valueOf(extInfo.type));
/* 1860 */             info.add(extInfo.info1);
/* 1861 */             info.add(extInfo.info2);
/* 1862 */             info.add(extInfo.info3);
/* 1863 */             info.add(extInfo.info4);
/* 1864 */             addParams.add(info.toArray(new Object[0]));
/*      */           }
/*      */         }
/*      */ 
/* 1868 */         if (updateParams.size() > 0) {
/* 1869 */           runner.batch(conn, "UPDATE T_Customer_Ext_Info SET Info1 = ?, Info2 = ?, Info3 = ?, Info4 = ? WHERE CustomerExtId = ?", 
/* 1870 */             (Object[][])updateParams.toArray(new Object[0][]));
/*      */         }
/*      */ 
/* 1873 */         if (addParams.size() > 0) {
/* 1874 */           runner.batch(conn, "INSERT INTO T_Customer_Ext_Info (CompanyId,CustomerId,Type,Info1,Info2,Info3,Info4,CreateDate) VALUES (?,?,?,?,?,?,?,NOW())", 
/* 1875 */             (Object[][])addParams.toArray(new Object[0][]));
/*      */         }
/*      */ 
/* 1878 */         if (((List)deleteParams).size() > 0) {
/* 1879 */           runner.batch(conn, "DELETE FROM T_Customer_Ext_Info WHERE CustomerExtId = ?", 
/* 1880 */             (Object[][])((List)deleteParams).toArray(new Object[0][]));
/*      */         }
/*      */       }
/*      */ 
/* 1884 */       if ((deleteExtInfoIds != null) && 
/* 1885 */         (deleteExtInfoIds.trim().length() > 0)) {
/* 1886 */         String deleteSQL = "DELETE FROM T_Customer_Ext_Info WHERE CustomerExtId IN (" + 
/* 1887 */           deleteExtInfoIds + ")";
/* 1888 */         runner.update(conn, deleteSQL);
/*      */       }
/*      */ 
/* 1891 */       return count;
/*      */     }
/*      */     catch (SQLException e)
/*      */     {
/* 1898 */       logger.warn("update people", e);
/*      */     } finally {
/*      */       try {
/* 1901 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 1903 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/* 1907 */     return 0;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getPeoples(int customerId)
/*      */   {
/* 1919 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 1920 */     if (conn == null) {
/* 1921 */       return null;
/*      */     }
/*      */ 
/* 1924 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 1926 */       return (List)runner.query(conn, "SELECT CustomerId,Name,Title,Visible,PermissionsId,ImgPath,CreateUserId FROM T_Customer_Info WHERE DeleteFlag = 0 AND ParentCustomerId = ? ", 
/* 1927 */         new MyMapListHandler(), new Object[] { Integer.valueOf(customerId) });
/*      */     } catch (SQLException e) {
/* 1929 */       logger.warn("get peoples info failed", e);
/*      */     } finally {
/*      */       try {
/* 1932 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 1934 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 1937 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getPeoplesMiniInfo(int customerId)
/*      */     throws DBException, SQLException
/*      */   {
/* 1950 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 1951 */     if (conn == null) {
/* 1952 */       throw new DBException();
/*      */     }
/*      */ 
/* 1955 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 1957 */       return (List)runner.query(conn, "SELECT CustomerId, Name FROM T_Customer_Info WHERE DeleteFlag = 0 AND ParentCustomerId = ? ", 
/* 1958 */         new MyMapListHandler(), new Object[] { Integer.valueOf(customerId) });
/*      */     } finally {
/*      */       try {
/* 1961 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 1963 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int changeAllVisibility(int changeVisible, String changePermissionsId, int limitNum, boolean deleteFlag, Date nowUpdate, CustomerSearchCondition searchCondition, User loginUser)
/*      */   {
/* 1984 */     if (searchCondition.companyId <= 0) {
/* 1985 */       return 0;
/*      */     }
/* 1987 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 1988 */     if (conn == null) {
/* 1989 */       return 0;
/*      */     }
/*      */ 
/* 1997 */     StringBuffer sql = new StringBuffer(
/* 1998 */       "UPDATE T_Customer_Info AS U INNER JOIN (SELECT DISTINCT A.CustomerId FROM T_Customer_Info AS A ");
/*      */ 
/* 2003 */     StringBuffer peopleSql = new StringBuffer(
/* 2004 */       "UPDATE T_Customer_Info AS U INNER JOIN (SELECT DISTINCT A.CustomerId FROM T_Customer_Info AS A ");
/*      */ 
/* 2006 */     StringBuffer deleteShareUserSql = new StringBuffer(
/* 2007 */       "DELETE D.* FROM T_Customer_Permissions_Info AS D INNER JOIN T_Customer_Info AS A ON D.CustomerId = A.CustomerId ");
/*      */ 
/* 2009 */     List params = new ArrayList();
/*      */ 
/* 2011 */     List whereParams = new ArrayList();
/* 2012 */     whereParams.add(Integer.valueOf(searchCondition.companyId));
/*      */ 
/* 2015 */     List sqls = getListCommonSQL(params, whereParams, 
/* 2016 */       searchCondition, loginUser, false);
/*      */ 
/* 2018 */     sql.append((StringBuffer)sqls.get(0))
/* 2019 */       .append(" WHERE A.DeleteFlag = 0 AND A.CompanyId = ? ")
/* 2020 */       .append((StringBuffer)sqls.get(1));
/* 2021 */     peopleSql.append((StringBuffer)sqls.get(0))
/* 2022 */       .append(" WHERE A.DeleteFlag = 0 AND A.CompanyId = ? ")
/* 2023 */       .append((StringBuffer)sqls.get(1));
/* 2024 */     deleteShareUserSql.append((StringBuffer)sqls.get(0))
/* 2025 */       .append(" WHERE A.DeleteFlag = 0 AND A.CompanyId = ? ")
/* 2026 */       .append((StringBuffer)sqls.get(1));
/*      */ 
/* 2029 */     if (searchCondition.filters.size() == 0) {
/* 2030 */       sql.append(" AND (A.Type = ? OR (A.Type = ? AND A.ParentCustomerId = -1)) ");
/* 2031 */       peopleSql
/* 2032 */         .append(" AND (A.Type = ? OR (A.Type = ? AND A.ParentCustomerId = -1)) ");
/* 2033 */       deleteShareUserSql
/* 2034 */         .append(" AND (A.Type = ? OR (A.Type = ? AND A.ParentCustomerId = -1)) ");
/*      */ 
/* 2036 */       whereParams.add(Integer.valueOf(0));
/* 2037 */       whereParams.add(Integer.valueOf(1));
/*      */     }
/*      */ 
/* 2041 */     if (limitNum > 0) {
/* 2042 */       sql.append("AND (A.AssignDate <> ? OR A.AssignDate IS NULL) LIMIT " + 
/* 2043 */         limitNum + 
/* 2044 */         " ) AS tmptable ON U.CustomerId = tmptable.CustomerId SET Visible = ?, PermissionsId = ?, UpdateUserId = ?, UpdateDate = NOW(), AssignDate = ?, IndexFlag = 0 ");
/* 2045 */       peopleSql
/* 2046 */         .append("AND A.AssignDate <> ? LIMIT " + 
/* 2047 */         limitNum + 
/* 2048 */         " ) AS tmptable ON U.ParentCustomerId = tmptable.CustomerId SET Visible = ?, PermissionsId = ?, UpdateUserId = ?, UpdateDate = NOW(), AssignDate = ?, IndexFlag = 0 ");
/*      */     } else {
/* 2050 */       sql.append("AND (A.AssignDate <> ? OR A.AssignDate IS NULL)) AS tmptable ON U.CustomerId = tmptable.CustomerId SET Visible = ?, PermissionsId = ?, UpdateUserId = ?, UpdateDate = NOW(), AssignDate = ?, IndexFlag = 0 ");
/* 2051 */       peopleSql
/* 2052 */         .append("AND A.AssignDate <> ? ) AS tmptable ON U.ParentCustomerId = tmptable.CustomerId SET Visible = ?, PermissionsId = ?, UpdateUserId = ?, UpdateDate = NOW(), AssignDate = ?, IndexFlag = 0 ");
/*      */     }
/*      */ 
/* 2057 */     if (changeVisible == 1) {
/* 2058 */       sql.append(", LastContactDate = NULL ");
/* 2059 */       peopleSql.append(", LastContactDate = NULL ");
/*      */     }
/*      */ 
/* 2062 */     params.addAll(whereParams);
/*      */ 
/* 2064 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 2068 */       if (deleteFlag) {
/* 2069 */         runner.update(conn, deleteShareUserSql.toString(), 
/* 2070 */           params.toArray());
/*      */       }
/*      */ 
/* 2075 */       params.add(nowUpdate);
/*      */ 
/* 2077 */       params.add(Integer.valueOf(changeVisible));
/* 2078 */       params.add(changePermissionsId);
/* 2079 */       params.add(searchCondition.userId);
/* 2080 */       params.add(nowUpdate);
/*      */ 
/* 2086 */       runner.update(conn, peopleSql.toString(), params.toArray());
/* 2087 */       return runner.update(conn, sql.toString(), params.toArray());
/*      */     } catch (SQLException e) {
/* 2089 */       logger.warn("change visibile failed", e);
/*      */     }
/*      */     finally
/*      */     {
/*      */       try
/*      */       {
/* 2097 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 2099 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 2102 */     return 0;
/*      */   }
/*      */ 
/*      */   public int changeVisibility(int customerId, int visible, String permissionsId, boolean isPerson, String userId)
/*      */     throws DBException, SQLException
/*      */   {
/* 2120 */     if (customerId <= 0) {
/* 2121 */       return 0;
/*      */     }
/*      */ 
/* 2124 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 2125 */     if (conn == null) {
/* 2126 */       throw new DBException();
/*      */     }
/*      */ 
/* 2129 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 2131 */       String sql = "UPDATE T_Customer_Info SET Visible = ?, PermissionsId = ?, UpdateUserId = ?, UpdateDate = NOW(), AssignDate = NOW(), IndexFlag = 0 WHERE CustomerId = ?";
/* 2132 */       if (visible == 1)
/*      */       {
/* 2134 */         sql = "UPDATE T_Customer_Info SET Visible = ?, PermissionsId = ?, UpdateUserId = ?, UpdateDate = NOW(), AssignDate = NOW(), IndexFlag = 0 WHERE CustomerId = ?";
/*      */       }
/* 2136 */       int result = runner.update(conn, sql, new Object[] { Integer.valueOf(visible), permissionsId, 
/* 2137 */         userId, Integer.valueOf(customerId) });
/*      */ 
/* 2140 */       runner.update(conn, "DELETE FROM T_Customer_Permissions_Info WHERE CustomerId = ? ", Integer.valueOf(customerId));
/* 2141 */       if ((result == 1) && 
/* 2142 */         (!isPerson)) {
/* 2143 */         sql = "UPDATE T_Customer_Info SET Visible = ?, PermissionsId = ?, UpdateUserId = ?, UpdateDate = NOW(), AssignDate = NOW(), IndexFlag = 0 WHERE ParentCustomerId = ?";
/* 2144 */         if (visible == 1) {
/* 2145 */           sql = "UPDATE T_Customer_Info SET Visible = ?, PermissionsId = ?, UpdateUserId = ?, UpdateDate = NOW(), AssignDate = NOW(), LastContactDate = NULL, IndexFlag = 0 WHERE ParentCustomerId = ?";
/*      */         }
/* 2147 */         runner.update(conn, sql, new Object[] { Integer.valueOf(visible), permissionsId, userId, 
/* 2148 */           Integer.valueOf(customerId) });
/*      */       }
/*      */ 
/* 2151 */       return result;
/*      */     } finally {
/*      */       try {
/* 2154 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 2156 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int changeCategoryId(int customerId, int categoryId, String userId)
/*      */     throws DBException, SQLException
/*      */   {
/* 2173 */     if (customerId <= 0) {
/* 2174 */       return 0;
/*      */     }
/*      */ 
/* 2177 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 2178 */     if (conn == null) {
/* 2179 */       throw new DBException();
/*      */     }
/*      */ 
/* 2182 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 2184 */       return runner.update(conn, "UPDATE T_Customer_Info SET CategoryId = ?, UpdateUserId = ?, UpdateDate = NOW() WHERE CustomerId = ? OR ParentCustomerId = ?", new Object[] { 
/* 2185 */         Integer.valueOf(categoryId), userId, Integer.valueOf(customerId), Integer.valueOf(customerId) });
/*      */     } finally {
/*      */       try {
/* 2188 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 2190 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int changeAllCategoryId(int categoryId, CustomerSearchCondition searchCondition, User loginUser)
/*      */   {
/* 2213 */     if (searchCondition.companyId <= 0) {
/* 2214 */       return 0;
/*      */     }
/* 2216 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 2217 */     if (conn == null) {
/* 2218 */       return 0;
/*      */     }
/*      */ 
/* 2228 */     StringBuffer sql = new StringBuffer(
/* 2229 */       "UPDATE T_Customer_Info AS U INNER JOIN (SELECT DISTINCT A.CustomerId FROM T_Customer_Info AS A ");
/*      */ 
/* 2231 */     StringBuffer personUpdatesql = new StringBuffer(
/* 2232 */       "UPDATE T_Customer_Info AS U INNER JOIN (SELECT DISTINCT A.CustomerId FROM T_Customer_Info AS A ");
/*      */ 
/* 2234 */     List params = new ArrayList();
/*      */ 
/* 2236 */     List whereParams = new ArrayList();
/* 2237 */     whereParams.add(Integer.valueOf(searchCondition.companyId));
/*      */ 
/* 2240 */     List sqls = getListCommonSQL(params, whereParams, 
/* 2241 */       searchCondition, loginUser, false);
/*      */ 
/* 2243 */     sql.append((StringBuffer)sqls.get(0))
/* 2244 */       .append(" WHERE A.DeleteFlag = 0 AND A.CompanyId = ? ")
/* 2245 */       .append((StringBuffer)sqls.get(1));
/* 2246 */     personUpdatesql.append((StringBuffer)sqls.get(0))
/* 2247 */       .append(" WHERE A.DeleteFlag = 0 AND A.CompanyId = ? ")
/* 2248 */       .append((StringBuffer)sqls.get(1));
/*      */ 
/* 2251 */     if (searchCondition.filters.size() == 0) {
/* 2252 */       sql.append(" AND (A.Type = ? OR (A.Type = ? AND A.ParentCustomerId = -1)) ");
/* 2253 */       whereParams.add(Integer.valueOf(0));
/* 2254 */       whereParams.add(Integer.valueOf(1));
/*      */     }
/*      */ 
/* 2257 */     sql.append(") AS tmptable ON U.CustomerId = tmptable.CustomerId SET U.CategoryId = ?, U.UpdateUserId = ?, U.UpdateDate = NOW() ");
/* 2258 */     personUpdatesql
/* 2259 */       .append(") AS tmptable ON U.ParentCustomerId = tmptable.CustomerId SET U.CategoryId = ?, U.UpdateUserId = ?, U.UpdateDate = NOW() ");
/* 2260 */     whereParams.add(Integer.valueOf(categoryId));
/* 2261 */     whereParams.add(searchCondition.userId);
/*      */ 
/* 2263 */     params.addAll(whereParams);
/* 2264 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 2266 */       int count = runner.update(conn, sql.toString(), params.toArray());
/*      */ 
/* 2268 */       if (count > 0) {
/* 2269 */         if (searchCondition.filters.size() == 0)
/*      */         {
/* 2271 */           params.remove(params.size() - 1);
/* 2272 */           params.remove(params.size() - 1);
/*      */         }
/* 2274 */         runner.update(conn, personUpdatesql.toString(), 
/* 2275 */           params.toArray());
/*      */       }
/* 2277 */       return count;
/*      */     } catch (SQLException e) {
/* 2279 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 2282 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 2284 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 2287 */     return 0;
/*      */   }
/*      */ 
/*      */   public int changeNextContactDate(int customerId, Date date, String userId)
/*      */     throws DBException, SQLException
/*      */   {
/* 2302 */     if (customerId <= 0) {
/* 2303 */       return 0;
/*      */     }
/*      */ 
/* 2306 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 2307 */     if (conn == null) {
/* 2308 */       throw new DBException();
/*      */     }
/*      */ 
/* 2311 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 2313 */       return runner.update(conn, 
/* 2314 */         "UPDATE T_Customer_Info SET NextContactDate = ?, UpdateUserId = ?, UpdateDate = NOW() WHERE CustomerId = ?", new Object[] { date, userId, 
/* 2315 */         Integer.valueOf(customerId) });
/*      */     } finally {
/*      */       try {
/* 2318 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 2320 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int changeAllNextContactDate(Date date, CustomerSearchCondition searchCondition, User loginUser)
/*      */   {
/* 2344 */     if (searchCondition.companyId <= 0) {
/* 2345 */       return 0;
/*      */     }
/* 2347 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 2348 */     if (conn == null) {
/* 2349 */       return 0;
/*      */     }
/*      */ 
/* 2355 */     StringBuffer sql = new StringBuffer(
/* 2356 */       "UPDATE T_Customer_Info AS U INNER JOIN (SELECT DISTINCT A.CustomerId FROM T_Customer_Info AS A ");
/*      */ 
/* 2358 */     List params = new ArrayList();
/*      */ 
/* 2360 */     List whereParams = new ArrayList();
/* 2361 */     whereParams.add(Integer.valueOf(searchCondition.companyId));
/*      */ 
/* 2364 */     List sqls = getListCommonSQL(params, whereParams, 
/* 2365 */       searchCondition, loginUser, false);
/*      */ 
/* 2367 */     sql.append((StringBuffer)sqls.get(0))
/* 2368 */       .append(" WHERE A.DeleteFlag = 0 AND A.CompanyId = ? ")
/* 2369 */       .append((StringBuffer)sqls.get(1));
/*      */ 
/* 2372 */     if (searchCondition.filters.size() == 0) {
/* 2373 */       sql.append(" AND (A.Type = ? OR (A.Type = ? AND A.ParentCustomerId = -1)) ");
/* 2374 */       whereParams.add(Integer.valueOf(0));
/* 2375 */       whereParams.add(Integer.valueOf(1));
/*      */     }
/*      */ 
/* 2378 */     sql.append(") AS tmptable ON U.CustomerId = tmptable.CustomerId SET NextContactDate = ?, UpdateUserId = ?, UpdateDate = NOW() ");
/* 2379 */     whereParams.add(date);
/* 2380 */     whereParams.add(searchCondition.userId);
/*      */ 
/* 2382 */     params.addAll(whereParams);
/*      */ 
/* 2384 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 2386 */       return runner.update(conn, sql.toString(), params.toArray());
/*      */     }
/*      */     catch (SQLException localSQLException1)
/*      */     {
/*      */     }
/*      */     finally
/*      */     {
/*      */       try
/*      */       {
/* 2396 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 2398 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 2401 */     return 0;
/*      */   }
/*      */ 
/*      */   private List<StringBuffer> getFilterSQL(List<String[]> filters, List<Object> params, List<Object> whereParams, int companyId)
/*      */   {
/* 2415 */     StringBuffer sql = new StringBuffer("");
/*      */ 
/* 2417 */     StringBuffer whereSQL = new StringBuffer("");
/*      */ 
/* 2419 */     int i = 0;
/* 2420 */     for (String[] filter : filters) {
/* 2421 */       String name = filter[0].trim();
/* 2422 */       int valueType = Util.str2Int(filter[1]);
/* 2423 */       String value = filter[2].trim();
/*      */ 
/* 2425 */       if ((name != "") && (value != ""))
/*      */       {
/* 2429 */         i++;
/*      */ 
/* 2432 */         if ("name".equals(name)) {
/* 2433 */           whereSQL.append(" AND A.Name like ?");
/* 2434 */           whereParams.add("%" + value + "%");
/*      */         }
/* 2438 */         else if ("title".equals(name)) {
/* 2439 */           whereSQL.append(" AND A.Title like ?");
/* 2440 */           whereParams.add("%" + value + "%");
/*      */         }
/* 2444 */         else if ("background".equals(name)) {
/* 2445 */           whereSQL.append(" AND A.Note like ?");
/* 2446 */           whereParams.add("%" + value + "%");
/*      */         }
/* 2451 */         else if ("im".equals(name)) {
/* 2452 */           sql.append(
/* 2453 */             " INNER JOIN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Type = ? AND Info1 like ?) AS Temp")
/* 2454 */             .append(i).append(" ON A.CustomerId = Temp").append(i)
/* 2455 */             .append(".CustomerId");
/*      */ 
/* 2457 */           params.add(Integer.valueOf(companyId));
/* 2458 */           params.add(Integer.valueOf(3));
/* 2459 */           params.add("%" + value + "%");
/*      */         }
/* 2464 */         else if ("email".equals(name)) {
/* 2465 */           sql.append(
/* 2466 */             " INNER JOIN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Type = ? AND Info1 like ?) AS Temp")
/* 2467 */             .append(i).append(" ON A.CustomerId = Temp").append(i)
/* 2468 */             .append(".CustomerId");
/*      */ 
/* 2471 */           params.add(Integer.valueOf(companyId));
/* 2472 */           params.add(Integer.valueOf(2));
/* 2473 */           params.add("%" + value + "%");
/*      */         }
/* 2478 */         else if ("phone".equals(name)) {
/* 2479 */           sql.append(
/* 2480 */             " INNER JOIN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Type = ? AND Info1 like ?) AS Temp")
/* 2481 */             .append(i).append(" ON A.CustomerId = Temp").append(i)
/* 2482 */             .append(".CustomerId");
/*      */ 
/* 2485 */           params.add(Integer.valueOf(companyId));
/* 2486 */           params.add(Integer.valueOf(1));
/* 2487 */           params.add("%" + value + "%");
/*      */         }
/* 2492 */         else if ("area".equals(name)) {
/* 2493 */           int len = value.split("_").length;
/* 2494 */           if (len >= 1)
/*      */           {
/* 2497 */             String province = value.split("_")[0];
/* 2498 */             String city = "";
/* 2499 */             if (len > 1) {
/* 2500 */               city = value.split("_")[1];
/*      */             }
/*      */ 
/* 2503 */             sql.append(" INNER JOIN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Type = ? ");
/*      */ 
/* 2509 */             params.add(Integer.valueOf(companyId));
/* 2510 */             params.add(Integer.valueOf(6));
/*      */ 
/* 2512 */             if ((province != null) && (province.length() > 0)) {
/* 2513 */               sql.append(" AND Info3 = ?");
/* 2514 */               params.add(province);
/*      */             }
/* 2516 */             if ((city != null) && (city.length() > 0)) {
/* 2517 */               sql.append(" AND Info4 = ?");
/* 2518 */               params.add(city);
/*      */             }
/* 2520 */             sql.append(" ) AS Temp").append(i)
/* 2521 */               .append(" ON A.CustomerId = Temp").append(i)
/* 2522 */               .append(".CustomerId");
/*      */           }
/*      */ 
/*      */         }
/* 2526 */         else if ("address".equals(name)) {
/* 2527 */           sql.append(
/* 2528 */             " INNER JOIN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Type = ? AND Info1 like ?) AS Temp")
/* 2529 */             .append(i).append(" ON A.CustomerId = Temp").append(i)
/* 2530 */             .append(".CustomerId");
/*      */ 
/* 2533 */           params.add(Integer.valueOf(companyId));
/* 2534 */           params.add(Integer.valueOf(6));
/* 2535 */           params.add("%" + value + "%");
/*      */         }
/* 2540 */         else if ("all".equals(name)) {
/* 2541 */           sql.append(
/* 2542 */             ", (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Info1 like ?) Temp")
/* 2543 */             .append(i);
/* 2544 */           whereSQL.append(
/* 2545 */             " AND (A.Name like ? OR A.Title like ? OR A.Note like ? OR A.CustomerId = Temp")
/* 2546 */             .append(i).append(".CustomerId)");
/*      */ 
/* 2549 */           params.add(Integer.valueOf(companyId));
/* 2550 */           params.add("%" + value + "%");
/*      */ 
/* 2552 */           whereParams.add("%" + value + "%");
/* 2553 */           whereParams.add("%" + value + "%");
/* 2554 */           whereParams.add("%" + value + "%");
/*      */         }
/* 2559 */         else if ("lastContactDate".equals(name)) {
/* 2560 */           String[] arr = value.split("~");
/* 2561 */           int len = arr.length;
/* 2562 */           if (len >= 1)
/*      */           {
/* 2565 */             Date startDate = Util.str2Date(arr[0]);
/* 2566 */             Date endDate = null;
/* 2567 */             if (len > 1) {
/* 2568 */               endDate = Util.str2Date(arr[1]);
/*      */             }
/* 2570 */             if (startDate != null) {
/* 2571 */               whereSQL.append(" AND DATEDIFF(A.LastContactDate, ?) >= 0 ");
/* 2572 */               whereParams.add(startDate);
/*      */             }
/* 2574 */             if (endDate != null) {
/* 2575 */               whereSQL.append(" AND DATEDIFF(A.LastContactDate, ?) <= 0 ");
/* 2576 */               whereParams.add(endDate);
/*      */             }
/*      */ 
/*      */           }
/*      */ 
/*      */         }
/* 2582 */         else if ("nextContactDate".equals(name)) {
/* 2583 */           String[] arr = value.split("~");
/* 2584 */           int len = arr.length;
/* 2585 */           if (len >= 1)
/*      */           {
/* 2588 */             Date startDate = Util.str2Date(arr[0]);
/* 2589 */             Date endDate = null;
/* 2590 */             if (len > 1) {
/* 2591 */               endDate = Util.str2Date(arr[1]);
/*      */             }
/* 2593 */             if (startDate != null) {
/* 2594 */               whereSQL.append(" AND DATEDIFF(A.NextContactDate, ?) >= 0 ");
/* 2595 */               whereParams.add(startDate);
/*      */             }
/* 2597 */             if (endDate != null) {
/* 2598 */               whereSQL.append(" AND DATEDIFF(A.NextContactDate, ?) <= 0 ");
/* 2599 */               whereParams.add(endDate);
/*      */             }
/*      */ 
/*      */           }
/*      */ 
/*      */         }
/* 2605 */         else if ("assignDate".equals(name)) {
/* 2606 */           String[] arr = value.split("~");
/* 2607 */           int len = arr.length;
/* 2608 */           if (len >= 1)
/*      */           {
/* 2611 */             Date startDate = Util.str2Date(arr[0]);
/* 2612 */             Date endDate = null;
/* 2613 */             if (len > 1) {
/* 2614 */               endDate = Util.str2Date(arr[1]);
/*      */             }
/* 2616 */             if (startDate != null) {
/* 2617 */               whereSQL.append(" AND DATEDIFF(A.AssignDate, ?) >= 0 ");
/* 2618 */               whereParams.add(startDate);
/*      */             }
/* 2620 */             if (endDate != null) {
/* 2621 */               whereSQL.append(" AND DATEDIFF(A.AssignDate, ?) <= 0 ");
/* 2622 */               whereParams.add(endDate);
/*      */             }
/*      */ 
/*      */           }
/*      */ 
/*      */         }
/* 2628 */         else if ("createDate".equals(name)) {
/* 2629 */           String[] arr = value.split("~");
/* 2630 */           int len = arr.length;
/* 2631 */           if (len >= 1)
/*      */           {
/* 2634 */             Date startDate = Util.str2Date(arr[0]);
/* 2635 */             Date endDate = null;
/* 2636 */             if (len > 1) {
/* 2637 */               endDate = Util.str2Date(arr[1]);
/*      */             }
/* 2639 */             if (startDate != null) {
/* 2640 */               whereSQL.append(" AND DATEDIFF(A.CreateDate, ?) >= 0 ");
/* 2641 */               whereParams.add(startDate);
/*      */             }
/* 2643 */             if (endDate != null) {
/* 2644 */               whereSQL.append(" AND DATEDIFF(A.CreateDate, ?) <= 0 ");
/* 2645 */               whereParams.add(endDate);
/*      */             }
/*      */ 
/*      */           }
/*      */ 
/*      */         }
/* 2651 */         else if ("createUserId".equals(name)) {
/* 2652 */           whereSQL.append(" AND A.CreateUserId = ? ");
/* 2653 */           whereParams.add(value);
/*      */         }
/* 2658 */         else if ("updateUserId".equals(name)) {
/* 2659 */           whereSQL.append(" AND A.UpdateUserId = ? ");
/* 2660 */           whereParams.add(value);
/*      */         }
/* 2665 */         else if (name.contains("field")) {
/* 2666 */           int type = 
/* 2667 */             Util.str2Int(name.substring("field".length()));
/* 2668 */           if (type >= 99)
/*      */           {
/* 2672 */             if (valueType == 1) {
/* 2673 */               if (value.indexOf("~") >= 0)
/*      */               {
/* 2677 */                 String[] arr = value.split("~");
/* 2678 */                 String start = arr[0].trim();
/* 2679 */                 String end = null;
/* 2680 */                 if (arr.length == 2) {
/* 2681 */                   end = arr[1].trim();
/*      */                 }
/*      */ 
/* 2684 */                 if ((Util.isNumeric(start)) || (Util.isNumeric(end)))
/*      */                 {
/* 2688 */                   sql.append(" INNER JOIN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Type = ? ");
/*      */ 
/* 2692 */                   params.add(Integer.valueOf(companyId));
/* 2693 */                   params.add(Integer.valueOf(type));
/* 2694 */                   if ((start != null) && (start.length() > 0)) {
/* 2695 */                     sql.append(" AND Info1 >= " + start);
/*      */                   }
/*      */ 
/* 2698 */                   if ((end != null) && (end.length() > 0)) {
/* 2699 */                     sql.append(" AND Info1 <= " + end);
/*      */                   }
/* 2701 */                   sql.append(") Temp").append(i)
/* 2702 */                     .append(" ON A.CustomerId = Temp").append(i)
/* 2703 */                     .append(".CustomerId");
/*      */                 }
/*      */               } } else if (valueType == 2) {
/* 2706 */               if (value.indexOf("~") >= 0)
/*      */               {
/* 2710 */                 String[] arr = value.split("~");
/* 2711 */                 String start = arr[0].trim();
/* 2712 */                 String end = null;
/* 2713 */                 if (arr.length == 2) {
/* 2714 */                   end = arr[1].trim();
/*      */                 }
/*      */ 
/* 2717 */                 sql.append(" INNER JOIN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Type = ? ");
/*      */ 
/* 2720 */                 params.add(Integer.valueOf(companyId));
/* 2721 */                 params.add(Integer.valueOf(type));
/* 2722 */                 if ((start != null) && (start.length() > 0)) {
/* 2723 */                   sql.append(" AND Info1 >= ? ");
/* 2724 */                   params.add(start);
/*      */                 }
/*      */ 
/* 2727 */                 if ((end != null) && (end.length() > 0)) {
/* 2728 */                   sql.append(" AND Info1 <= ? ");
/* 2729 */                   params.add(end);
/*      */                 }
/* 2731 */                 sql.append(") AS Temp").append(i)
/* 2732 */                   .append(" ON A.CustomerId = Temp").append(i)
/* 2733 */                   .append(".CustomerId");
/*      */               } } else if (valueType == 3) {
/* 2735 */               sql.append(
/* 2736 */                 " INNER JOIN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Type = ? AND Info1 = ?) AS Temp")
/* 2737 */                 .append(i).append(" ON A.CustomerId = Temp")
/* 2738 */                 .append(i).append(".CustomerId");
/*      */ 
/* 2740 */               params.add(Integer.valueOf(companyId));
/* 2741 */               params.add(Integer.valueOf(type));
/* 2742 */               params.add(value);
/*      */             }
/*      */             else
/*      */             {
/* 2746 */               sql.append(
/* 2747 */                 " INNER JOIN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Type = ? AND Info1 like ?) AS Temp")
/* 2748 */                 .append(i).append(" ON A.CustomerId = Temp")
/* 2749 */                 .append(i).append(".CustomerId");
/*      */ 
/* 2751 */               params.add(Integer.valueOf(companyId));
/* 2752 */               params.add(Integer.valueOf(type));
/* 2753 */               params.add("%" + value + "%");
/*      */             }
/*      */           }
/*      */         }
/*      */       }
/*      */     }
/*      */ 
/* 2760 */     List result = new ArrayList();
/* 2761 */     result.add(sql);
/* 2762 */     result.add(whereSQL);
/*      */ 
/* 2764 */     return result;
/*      */   }
/*      */ 
/*      */   public int deleteCustomers(String customerIds, int companyId, User loginUser)
/*      */   {
/* 2776 */     if ((customerIds == null) || (customerIds.trim().length() == 0)) {
/* 2777 */       return 0;
/*      */     }
/* 2779 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 2780 */     if (conn == null) {
/* 2781 */       return 0;
/*      */     }
/*      */ 
/* 2784 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*      */       int i;
/* 2786 */       if (loginUser.isAdmin) {
/* 2787 */         String sql = "UPDATE T_Customer_Info SET DeleteFlag = 1, IndexFlag = 0 WHERE CompanyId = ? AND CustomerId IN (" + customerIds + 
/* 2788 */           ")";
/* 2789 */         return runner.update(conn, sql, Integer.valueOf(companyId));
/*      */       }
/*      */ 
/* 2792 */       String sql = "UPDATE T_Customer_Info SET DeleteFlag = 1, IndexFlag = 0 WHERE CompanyId = ? AND Visible = ? AND PermissionsId = ? AND CustomerId IN (" + customerIds + ")";
/* 2793 */       return runner.update(conn, sql, new Object[] { Integer.valueOf(companyId), Integer.valueOf(4), 
/* 2794 */         loginUser.userId });
/*      */     } catch (SQLException e) {
/* 2796 */       logger.warn("delete", e);
/*      */     } finally {
/*      */       try {
/* 2799 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 2801 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 2804 */     return 0;
/*      */   }
/*      */ 
/*      */   public int deleteCustomer(int customerId, boolean isAdmin, String operateUserId, int companyId)
/*      */     throws DBException
/*      */   {
/* 2819 */     if ((customerId <= 0) || (operateUserId == null) || 
/* 2820 */       (operateUserId.trim().length() == 0)) {
/* 2821 */       return 0;
/*      */     }
/*      */ 
/* 2824 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 2825 */     if (conn == null) {
/* 2826 */       throw new DBException();
/*      */     }
/*      */ 
/* 2829 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 2832 */       if (isAdmin) {
/* 2833 */         int i = runner.update(conn, "UPDATE T_Customer_Info SET DeleteFlag = 1, IndexFlag = 0 WHERE CompanyId = ? AND CustomerId = ? ", new Object[] { 
/* 2834 */           Integer.valueOf(companyId), Integer.valueOf(customerId) });
/*      */ 
/* 2833 */         return i;
/*      */       }
/*      */ 
/* 2838 */       runner.update(conn, "UPDATE T_Customer_Info SET DeleteFlag = 1, IndexFlag = 0 WHERE CompanyId = ? AND CustomerId = ? AND Visible = ? AND PermissionsId = ? ", new Object[] { Integer.valueOf(companyId), 
/* 2839 */         Integer.valueOf(customerId), Integer.valueOf(4), operateUserId });
/*      */     } catch (SQLException e) {
/* 2841 */       logger.warn("delete customer failed", e);
/*      */       try
/*      */       {
/* 2844 */         DbUtils.close(conn);
/*      */       } catch (SQLException se) {
/* 2846 */         logger.warn("close conn failed", se);
/*      */       }
/*      */     }
/*      */     finally
/*      */     {
/*      */       try
/*      */       {
/* 2844 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 2846 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 2849 */     return 0;
/*      */   }
/*      */ 
/*      */   public int deleteSelectAllCustomers(CustomerSearchCondition searchCondition, User loginUser)
/*      */   {
/* 2865 */     if (searchCondition.companyId <= 0) {
/* 2866 */       return 0;
/*      */     }
/* 2868 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 2869 */     if (conn == null) {
/* 2870 */       return 0;
/*      */     }
/*      */ 
/* 2873 */     List params = new ArrayList();
/* 2874 */     List whereParams = new ArrayList();
/* 2875 */     whereParams.add(Integer.valueOf(searchCondition.companyId));
/*      */ 
/* 2877 */     List sqls = getListCommonSQL(params, whereParams, 
/* 2878 */       searchCondition, loginUser, false);
/*      */ 
/* 2880 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*      */       int i;
/* 2882 */       if (loginUser.isAdmin) {
/* 2883 */         StringBuffer sql = new StringBuffer(
/* 2884 */           "UPDATE T_Customer_Info AS A  ");
/* 2885 */         sql.append((StringBuffer)sqls.get(0))
/* 2886 */           .append(" SET A.DeleteFlag = 1, A.IndexFlag = 0 WHERE A.CompanyId = ? ")
/* 2887 */           .append((StringBuffer)sqls.get(1));
/*      */ 
/* 2890 */         if (searchCondition.filters.size() == 0) {
/* 2891 */           sql.append(" AND (A.Type = ? OR (A.Type = ? AND A.ParentCustomerId = -1)) ");
/* 2892 */           whereParams.add(Integer.valueOf(0));
/* 2893 */           whereParams.add(Integer.valueOf(1));
/*      */         }
/* 2895 */         params.addAll(whereParams);
/* 2896 */         return runner.update(conn, sql.toString(), params.toArray());
/*      */       }
/*      */ 
/* 2900 */       StringBuffer sql = new StringBuffer("UPDATE T_Customer_Info AS A  ");
/* 2901 */       sql.append((StringBuffer)sqls.get(0))
/* 2902 */         .append(" SET A.DeleteFlag = 1, A.IndexFlag = 0 WHERE A.CompanyId = ? ")
/* 2903 */         .append((StringBuffer)sqls.get(1))
/* 2904 */         .append(" AND A.Visible = ? AND A.PermissionsId = ?");
/*      */ 
/* 2906 */       whereParams.add(Integer.valueOf(4));
/* 2907 */       whereParams.add(loginUser.userId);
/*      */ 
/* 2910 */       if (searchCondition.filters.size() == 0) {
/* 2911 */         sql.append(" AND (A.Type = ? OR (A.Type = ? AND A.ParentCustomerId = -1)) ");
/* 2912 */         whereParams.add(Integer.valueOf(0));
/* 2913 */         whereParams.add(Integer.valueOf(1));
/*      */       }
/*      */ 
/* 2916 */       params.addAll(whereParams);
/* 2917 */       return runner.update(conn, sql.toString(), params.toArray());
/*      */     } catch (SQLException e) {
/* 2919 */       logger.warn("get deleteSelectAll failed", e);
/*      */     } finally {
/*      */       try {
/* 2922 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 2924 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 2927 */     return 0;
/*      */   }
/*      */ 
/*      */   public int removeShare(String customerIds, String loginUserId)
/*      */     throws DBException
/*      */   {
/* 2935 */     if ((customerIds == null) || (customerIds.trim().length() == 0)) {
/* 2936 */       return 0;
/*      */     }
/*      */ 
/* 2939 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 2940 */     if (conn == null) {
/* 2941 */       throw new DBException();
/*      */     }
/*      */ 
/* 2944 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 2946 */       return runner.update(conn, "DELETE FROM T_Customer_Permissions_Info WHERE OwnerUserId = ? AND CustomerId IN ( " + 
/* 2947 */         customerIds + ")", loginUserId);
/*      */     } catch (SQLException e) {
/* 2949 */       logger.warn("remove share failed", e);
/*      */     } finally {
/*      */       try {
/* 2952 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 2954 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 2957 */     return 0;
/*      */   }
/*      */ 
/*      */   public int removeShareSelectAll(CustomerSearchCondition searchCondition, User loginUser)
/*      */   {
/* 2974 */     if (searchCondition.companyId <= 0) {
/* 2975 */       return 0;
/*      */     }
/* 2977 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 2978 */     if (conn == null) {
/* 2979 */       return 0;
/*      */     }
/*      */ 
/* 2982 */     List params = new ArrayList();
/* 2983 */     List whereParams = new ArrayList();
/* 2984 */     whereParams.add(loginUser.userId);
/* 2985 */     whereParams.add(Integer.valueOf(searchCondition.companyId));
/*      */ 
/* 2988 */     List sqls = getListCommonSQL(params, whereParams, 
/* 2989 */       searchCondition, loginUser, true);
/*      */ 
/* 2994 */     StringBuffer sql = new StringBuffer(
/* 2995 */       "DELETE D.* FROM T_Customer_Permissions_Info AS D INNER JOIN T_Customer_Info AS A ON D.CustomerId = A.CustomerId ");
/*      */ 
/* 2997 */     sql.append((StringBuffer)sqls.get(0))
/* 2998 */       .append(" WHERE D.OwnerUserId = ? AND A.DeleteFlag = 0 AND A.CompanyId = ? ")
/* 2999 */       .append((StringBuffer)sqls.get(1));
/*      */ 
/* 3002 */     if (searchCondition.filters.size() == 0) {
/* 3003 */       sql.append(" AND (A.Type = ? OR (A.Type = ? AND A.ParentCustomerId = -1)) ");
/* 3004 */       whereParams.add(Integer.valueOf(0));
/* 3005 */       whereParams.add(Integer.valueOf(1));
/*      */     }
/*      */ 
/* 3008 */     params.addAll(whereParams);
/*      */ 
/* 3012 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 3014 */       return runner.update(conn, sql.toString(), params.toArray());
/*      */     } catch (SQLException e) {
/* 3016 */       logger.warn("get remove Share SelectAll failed", e);
/*      */     } finally {
/*      */       try {
/* 3019 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 3021 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 3024 */     return 0;
/*      */   }
/*      */ 
/*      */   public int addShare(String customerIds, String operationUserId, String shareUserIds, String shareGroupIds)
/*      */   {
/* 3038 */     if (((shareUserIds != null) && (shareUserIds.trim().length() != 0)) || ((shareGroupIds == null) || 
/* 3039 */       (shareGroupIds
/* 3039 */       .trim().length() == 0) || 
/* 3040 */       (customerIds == null) || 
/* 3041 */       (customerIds.trim().length() == 0) || 
/* 3042 */       (operationUserId == null) || 
/* 3043 */       (operationUserId.trim().length() == 0))) {
/* 3044 */       return 0;
/*      */     }
/*      */ 
/* 3047 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 3048 */     if (conn == null) {
/* 3049 */       return 0;
/*      */     }
/*      */ 
/* 3052 */     List params = new ArrayList();
/*      */ 
/* 3054 */     for (String id : customerIds.split(",")) {
/* 3055 */       if ((shareUserIds != null) && (shareUserIds.length() > 0)) {
/* 3056 */         for (String shareUserId : shareUserIds.split(","))
/* 3057 */           if (shareUserId.trim().length() != 0)
/*      */           {
/* 3061 */             if (!operationUserId.equals(shareUserId.trim()))
/*      */             {
/* 3065 */               List info = new ArrayList();
/* 3066 */               info.add(id);
/* 3067 */               info.add(operationUserId);
/* 3068 */               info.add(shareUserId.trim());
/* 3069 */               info.add(Integer.valueOf(1));
/*      */ 
/* 3071 */               params.add(info.toArray(new Object[0]));
/*      */             }
/*      */           }
/*      */       }
/* 3075 */       if ((shareGroupIds != null) && (shareGroupIds.length() > 0)) {
/* 3076 */         for (String shareGroupId : shareGroupIds.split(",")) {
/* 3077 */           if (shareGroupId.trim().length() != 0)
/*      */           {
/* 3080 */             List info = new ArrayList();
/* 3081 */             info.add(id);
/* 3082 */             info.add(operationUserId);
/* 3083 */             info.add(shareGroupId.trim());
/* 3084 */             info.add(Integer.valueOf(2));
/*      */ 
/* 3086 */             params.add(info.toArray(new Object[0]));
/*      */           }
/*      */         }
/*      */       }
/*      */     }
/* 3091 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 3094 */       int[] result = runner.batch(conn, "INSERT IGNORE INTO T_Customer_Permissions_Info (CustomerId,OwnerUserId,ShareId,ShareType,CreateDate) VALUES (?,?,?,?,NOW())", 
/* 3095 */         (Object[][])params.toArray(new Object[0][]));
/* 3096 */       int num = 0;
/* 3097 */       for (int i = 0; i < result.length; i++) {
/* 3098 */         if (result[i] == 1) {
/* 3099 */           num++;
/*      */         }
/*      */       }
/* 3102 */       return num;
/*      */     }
/*      */     catch (SQLException e) {
/* 3105 */       logger.warn("add customer share info failed", e);
/*      */     } finally {
/*      */       try {
/* 3108 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 3110 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 3113 */     return 0;
/*      */   }
/*      */ 
/*      */   public int addShareSelectAll(CustomerSearchCondition searchCondition, String shareId, User loginUser, int shareType)
/*      */   {
/* 3133 */     if (searchCondition.companyId <= 0) {
/* 3134 */       return 0;
/*      */     }
/* 3136 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 3137 */     if (conn == null) {
/* 3138 */       return 0;
/*      */     }
/*      */ 
/* 3141 */     List params = new ArrayList();
/* 3142 */     List whereParams = new ArrayList();
/* 3143 */     whereParams.add(Integer.valueOf(searchCondition.companyId));
/*      */ 
/* 3146 */     List sqls = getListCommonSQL(params, whereParams, 
/* 3147 */       searchCondition, loginUser, false);
/*      */ 
/* 3149 */     StringBuffer sql = new StringBuffer(
/* 3150 */       "INSERT IGNORE INTO T_Customer_Permissions_Info (CustomerId, OwnerUserId, ShareId, ShareType, CreateDate) SELECT DISTINCT A.CustomerId, ");
/* 3151 */     sql.append("'").append(searchCondition.userId).append("', '")
/* 3152 */       .append(shareId).append("', ").append(shareType)
/* 3153 */       .append(", NOW() FROM T_Customer_Info AS A ")
/* 3154 */       .append((StringBuffer)sqls.get(0))
/* 3155 */       .append(" WHERE A.DeleteFlag = 0 AND A.CompanyId = ? ");
/* 3156 */     sql.append((StringBuffer)sqls.get(1));
/*      */ 
/* 3159 */     if (searchCondition.filters.size() == 0) {
/* 3160 */       sql.append(" AND (A.Type = ? OR (A.Type = ? AND A.ParentCustomerId = -1)) ");
/* 3161 */       whereParams.add(Integer.valueOf(0));
/* 3162 */       whereParams.add(Integer.valueOf(1));
/*      */     }
/* 3164 */     params.addAll(whereParams);
/*      */ 
/* 3166 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 3168 */       return runner.update(conn, sql.toString(), params.toArray());
/*      */     } catch (SQLException e) {
/* 3170 */       logger.warn("add share all failed", e);
/*      */     } finally {
/*      */       try {
/* 3173 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 3175 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 3178 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getTotalNum(CustomerSearchCondition searchCondition, User loginUser)
/*      */   {
/* 3197 */     if (searchCondition.companyId <= 0) {
/* 3198 */       return 0;
/*      */     }
/* 3200 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 3201 */     if (conn == null) {
/* 3202 */       return 0;
/*      */     }
/*      */ 
/* 3205 */     StringBuffer sql = new StringBuffer(
/* 3206 */       "SELECT COUNT(DISTINCT(A.CustomerId)) FROM T_Customer_Info AS A ");
/*      */ 
/* 3208 */     List params = new ArrayList();
/* 3209 */     List whereParams = new ArrayList();
/* 3210 */     whereParams.add(Integer.valueOf(searchCondition.companyId));
/*      */ 
/* 3213 */     List sqls = getListCommonSQL(params, whereParams, 
/* 3214 */       searchCondition, loginUser, false);
/*      */ 
/* 3216 */     sql.append((StringBuffer)sqls.get(0)).append(
/* 3217 */       " WHERE A.DeleteFlag = 0 AND A.CompanyId = ? ");
/* 3218 */     sql.append((StringBuffer)sqls.get(1));
/*      */ 
/* 3221 */     if (searchCondition.filters.size() == 0) {
/* 3222 */       sql.append(" AND (A.Type = ? OR (A.Type = ? AND A.ParentCustomerId = -1)) ");
/* 3223 */       whereParams.add(Integer.valueOf(0));
/* 3224 */       whereParams.add(Integer.valueOf(1));
/*      */     }
/*      */ 
/* 3227 */     params.addAll(whereParams);
/*      */ 
/* 3229 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 3231 */       Object obj = runner.query(conn, sql.toString(), 
/* 3232 */         new ScalarHandler(1), params.toArray());
/* 3233 */       if (!(obj instanceof Long)) {
/* 3234 */         return 0;
/*      */       }
/* 3236 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/* 3238 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 3241 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 3243 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 3246 */     return 0;
/*      */   }
/*      */ 
/*      */   private List<StringBuffer> getListCommonSQL(List<Object> params, List<Object> whereParams, CustomerSearchCondition searchCondition, User loginUser, boolean isRemoveShareSQL)
/*      */   {
/* 3270 */     StringBuffer sql = new StringBuffer();
/*      */ 
/* 3273 */     StringBuffer whereSQL = new StringBuffer();
/*      */ 
/* 3276 */     if ((searchCondition.categoryIds != null) && 
/* 3277 */       (searchCondition.categoryIds.trim().length() > 0)) {
/* 3278 */       whereSQL.append(" AND A.CategoryId IN (")
/* 3279 */         .append(searchCondition.categoryIds).append(")");
/*      */     }
/*      */ 
/* 3282 */     if (!isRemoveShareSQL)
/*      */     {
/* 3286 */       if (searchCondition.visible == 1)
/*      */       {
/* 3288 */         whereParams.add(Integer.valueOf(1));
/* 3289 */         whereSQL.append(" AND A.Visible = ? ");
/*      */ 
/* 3292 */         if ("*"
/* 3292 */           .equals(searchCondition.permissionsId))
/*      */         {
/* 3294 */           if (!loginUser.isAdmin)
/*      */           {
/* 3296 */             whereSQL.append(" AND A.PermissionsId = ? ");
/* 3297 */             whereParams.add(Integer.valueOf(loginUser.groupId));
/*      */           }
/*      */         } else {
/* 3300 */           whereSQL.append(" AND A.PermissionsId = ? ");
/* 3301 */           whereParams.add(searchCondition.permissionsId);
/*      */         }
/* 3303 */       } else if (searchCondition.visible == 4)
/*      */       {
/* 3305 */         whereParams.add(Integer.valueOf(4));
/*      */ 
/* 3307 */         if ("*"
/* 3307 */           .equals(searchCondition.permissionsId)) {
/* 3308 */           if (loginUser.isAdmin)
/*      */           {
/* 3310 */             whereSQL.append(" AND A.Visible = ? ");
/*      */           } else {
/* 3312 */             whereSQL.append(" AND A.Visible = ? AND A.PermissionsId = ? ");
/* 3313 */             whereParams.add(loginUser.userId);
/*      */           }
/*      */         } else {
/* 3316 */           whereSQL.append(" AND A.Visible = ? AND A.PermissionsId = ? ");
/* 3317 */           whereParams.add(searchCondition.permissionsId);
/*      */         }
/*      */       }
/* 3320 */       else if (searchCondition.visible == 3)
/*      */       {
/* 3322 */         if (searchCondition.childGroupIds.indexOf(",") > 0) {
/* 3323 */           whereSQL.append(" AND A.Visible = ? AND A.PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 3324 */             searchCondition.childGroupIds + "))");
/* 3325 */           whereParams.add(Integer.valueOf(4));
/* 3326 */           whereParams.add(Integer.valueOf(searchCondition.companyId));
/*      */         } else {
/* 3328 */           whereSQL.append(" AND A.Visible = ? AND A.PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId = ?) ");
/* 3329 */           whereParams.add(Integer.valueOf(4));
/* 3330 */           whereParams.add(Integer.valueOf(searchCondition.companyId));
/* 3331 */           whereParams.add(Integer.valueOf(searchCondition.groupId));
/*      */         }
/* 3333 */       } else if (searchCondition.visible == 5)
/*      */       {
/* 3335 */         sql.append(" INNER JOIN T_Customer_Permissions_Info AS D ON A.CustomerId = D.CustomerId AND D.OwnerUserId = ? ");
/* 3336 */         params.add(loginUser.userId);
/*      */       }
/* 3367 */       else if (searchCondition.visible == 6)
/*      */       {
/* 3373 */         sql.append(" INNER JOIN T_Customer_Permissions_Info AS D ON A.CustomerId = D.CustomerId AND ((D.ShareId = ? AND D.ShareType = ? ) OR ( D.ShareId IN ( SELECT GroupId FROM T_User_Info WHERE UserId = ? ) AND D.ShareType = ? )) ");
/* 3374 */         params.add(loginUser.userId);
/* 3375 */         params.add(Integer.valueOf(1));
/* 3376 */         params.add(loginUser.userId);
/* 3377 */         params.add(Integer.valueOf(2));
/*      */       }
/*      */ 
/*      */     }
/*      */ 
/* 3416 */     if (searchCondition.notContactType > 0)
/* 3417 */       if (searchCondition.notContactType == 1)
/*      */       {
/* 3419 */         whereSQL.append(" AND A.LastContactDate IS NULL ");
/* 3420 */       } else if (searchCondition.notContactType == 2)
/*      */       {
/* 3423 */         whereSQL.append(" AND DATEDIFF(NOW(), A.LastContactDate) >= ? ");
/* 3424 */         whereParams.add(Integer.valueOf(7));
/* 3425 */       } else if (searchCondition.notContactType == 3)
/*      */       {
/* 3428 */         whereSQL.append(" AND DATEDIFF(NOW(), A.LastContactDate) >= ? ");
/* 3429 */         whereParams.add(Integer.valueOf(14));
/* 3430 */       } else if (searchCondition.notContactType == 4)
/*      */       {
/* 3433 */         whereSQL.append(" AND DATEDIFF(NOW(), A.LastContactDate) >= ? ");
/* 3434 */         whereParams.add(Integer.valueOf(30));
/*      */       }
/*      */     int day;
/*      */     int start;
/* 3439 */     if (searchCondition.contactType > 0) {
/* 3440 */       if (searchCondition.contactType == 1)
/*      */       {
/* 3443 */         whereSQL.append(" AND DATEDIFF(NOW(), A.LastContactDate) = ? ");
/* 3444 */         whereParams.add(Integer.valueOf(0));
/* 3445 */       } else if (searchCondition.contactType == 2)
/*      */       {
/* 3447 */         whereSQL.append(" AND DATEDIFF(NOW(), A.LastContactDate) = ? ");
/* 3448 */         whereParams.add(Integer.valueOf(1));
/* 3449 */       } else if (searchCondition.contactType == 3)
/*      */       {
/* 3452 */         Calendar calendar = new GregorianCalendar();
/* 3453 */         calendar.setTime(new Date());
/* 3454 */         calendar.setFirstDayOfWeek(2);
/* 3455 */         int weekday = calendar.get(7) - 1;
/* 3456 */         if (weekday == 0) {
/* 3457 */           weekday = 7;
/*      */         }
/* 3459 */         start = weekday - 1;
/* 3460 */         whereSQL.append(" AND DATEDIFF(NOW(), A.LastContactDate) <= ? ");
/* 3461 */         whereParams.add(Integer.valueOf(start));
/* 3462 */       } else if (searchCondition.contactType == 4)
/*      */       {
/* 3465 */         Calendar calendar = new GregorianCalendar();
/* 3466 */         calendar.setTime(new Date());
/* 3467 */         calendar.setFirstDayOfWeek(2);
/* 3468 */         int weekday = calendar.get(7) - 1;
/* 3469 */         if (weekday == 0) {
/* 3470 */           weekday = 7;
/*      */         }
/* 3472 */         start = weekday + 6;
/* 3473 */         whereSQL.append(" AND DATEDIFF(NOW(), A.LastContactDate) <= ? ");
/* 3474 */         whereParams.add(Integer.valueOf(start));
/* 3475 */       } else if (searchCondition.contactType == 5)
/*      */       {
/* 3477 */         Calendar calendar = new GregorianCalendar();
/* 3478 */         calendar.setTime(new Date());
/* 3479 */         day = calendar.get(5);
/* 3480 */         start = day - 1;
/* 3481 */         whereSQL.append(" AND DATEDIFF(NOW(), A.LastContactDate) <= ? ");
/* 3482 */         whereParams.add(Integer.valueOf(start));
/*      */       }
/*      */ 
/*      */     }
/*      */ 
/* 3487 */     if ((searchCondition.tagIds != null) && 
/* 3488 */       (searchCondition.tagIds.length() > 0)) {
/* 3489 */       for (String tagId : searchCondition.tagIds.split(",")) {
/* 3490 */         int id = Util.str2Int(tagId);
/* 3491 */         if (id > 0)
/*      */         {
/* 3494 */           whereSQL.append(" AND A.CustomerId IN (SELECT CustomerId FROM T_Customer_Tag_Info WHERE TagId = ? )");
/* 3495 */           whereParams.add(Integer.valueOf(id));
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/*      */     }
/*      */ 
/* 3502 */     if (searchCondition.importId > 0) {
/* 3503 */       whereSQL.append(" AND A.ImportId = ? ");
/* 3504 */       whereParams.add(Integer.valueOf(searchCondition.importId));
/*      */     }
/*      */ 
/* 3507 */     List sqls = getFilterSQL(searchCondition.filters, 
/* 3508 */       params, whereParams, searchCondition.companyId);
/*      */ 
/* 3510 */     sql.append((StringBuffer)sqls.get(0));
/* 3511 */     whereSQL.append((StringBuffer)sqls.get(1));
/*      */ 
/* 3513 */     sqls.clear();
/* 3514 */     sqls.add(sql);
/* 3515 */     sqls.add(whereSQL);
/* 3516 */     return sqls;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getList(Connection conn, CustomerSearchCondition searchCondition, int offset, int pageSize, User loginUser, boolean isExport)
/*      */     throws DBException
/*      */   {
/* 3539 */     if (conn == null) {
/* 3540 */       return null;
/*      */     }
/*      */ 
/* 3543 */     List params = new ArrayList();
/* 3544 */     List whereParams = new ArrayList();
/* 3545 */     whereParams.add(Integer.valueOf(searchCondition.companyId));
/*      */ 
/* 3547 */     List sqls = getListCommonSQL(params, whereParams, 
/* 3548 */       searchCondition, loginUser, false);
/*      */ 
/* 3550 */     StringBuffer sql = null;
/* 3551 */     if (isExport)
/*      */     {
/* 3554 */       sql = new StringBuffer(
/* 3555 */         "SELECT DISTINCT A.UpdateDate, A.CustomerId, A.Name, C.CategoryName, A.Type, A.Title, A.Visible, A.PermissionsId, A.LastContactDate, A.NextContactDate, A.ParentCustomerId, A.CreateDate, A.Note FROM T_Customer_Info AS A LEFT JOIN T_Customer_Category AS C ON A.CategoryId = C.CategoryId ");
/*      */     }
/* 3557 */     else sql = new StringBuffer(
/* 3558 */         "SELECT DISTINCT A.CustomerId, A.Name, A.CategoryId, A.Type, A.Title, A.Visible, A.PermissionsId, A.LastContactDate, A.NextContactDate, A.ParentCustomerId, A.CreateDate, A.CreateUserId, A.LastNoteId FROM T_Customer_Info AS A ");
/*      */ 
/* 3560 */     sql.append((StringBuffer)sqls.get(0)).append(
/* 3561 */       " WHERE A.DeleteFlag = 0 AND A.CompanyId = ? ");
/* 3562 */     sql.append((StringBuffer)sqls.get(1));
/*      */ 
/* 3565 */     if (searchCondition.filters.size() == 0) {
/* 3566 */       sql.append(" AND (A.Type = ? OR (A.Type = ? AND A.ParentCustomerId = -1)) ");
/* 3567 */       whereParams.add(Integer.valueOf(0));
/* 3568 */       whereParams.add(Integer.valueOf(1));
/*      */     }
/*      */ 
/* 3571 */     if ("Name".equals(searchCondition.sortField))
/* 3572 */       sql.append(" ORDER BY A.Name ASC, A.lastContactDate DESC, A.CreateDate DESC");
/* 3573 */     else if ("LastContactDate".equals(searchCondition.sortField))
/* 3574 */       sql.append(" ORDER BY A.lastContactDate DESC, A.CreateDate DESC");
/* 3575 */     else if ("CreateDate".equals(searchCondition.sortField))
/* 3576 */       sql.append(" ORDER BY A.CreateDate DESC, A.lastContactDate DESC");
/*      */     else {
/* 3578 */       sql.append(" ORDER BY A.lastContactDate DESC, A.CreateDate DESC");
/*      */     }
/*      */ 
/* 3581 */     params.addAll(whereParams);
/* 3582 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 3584 */       if (pageSize > 0) {
/* 3585 */         sql.append(" LIMIT ").append(offset).append(", ")
/* 3586 */           .append(pageSize);
/*      */       }
/*      */ 
/* 3589 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 3590 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 3592 */       logger.warn("show all customers failed", e);
/* 3593 */     }throw new DBException();
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> search(int companyId, String companyName, String tel, String email, String qq)
/*      */     throws DBException
/*      */   {
/* 3610 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 3611 */     if (conn == null) {
/* 3612 */       return null;
/*      */     }
/*      */ 
/* 3615 */     List params = new ArrayList();
/* 3616 */     StringBuffer sql = null;
/*      */ 
/* 3618 */     sql = new StringBuffer(
/* 3619 */       "SELECT A.CustomerId, A.Name, A.CategoryId, A.Type, A.Title, A.Visible, A.PermissionsId, A.LastContactDate, A.ParentCustomerId, A.CreateDate, B.Name AS ParentCustomerName, A.CreateUserId FROM T_Customer_Info AS A LEFT JOIN T_Customer_Info AS B ON A.ParentCustomerId = B.CustomerId WHERE A.DeleteFlag = 0 AND A.CompanyId = ? ");
/* 3620 */     params.add(Integer.valueOf(companyId));
/* 3621 */     if ((companyName != null) && (companyName.trim().length() > 0)) {
/* 3622 */       sql.append(" AND A.Name like ?");
/* 3623 */       params.add("%" + companyName + "%");
/*      */     }
/*      */ 
/* 3627 */     if ((tel != null) && (tel.trim().length() > 0)) {
/* 3628 */       sql.append(" AND A.CustomerId IN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Type = ? AND Info1 like ? )");
/* 3629 */       params.add(Integer.valueOf(companyId));
/* 3630 */       params.add(Integer.valueOf(1));
/* 3631 */       params.add("%" + tel + "%");
/*      */     }
/*      */ 
/* 3635 */     if ((email != null) && (email.trim().length() > 0)) {
/* 3636 */       sql.append(" AND A.CustomerId IN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Type = ? AND Info1 like ? )");
/* 3637 */       params.add(Integer.valueOf(companyId));
/* 3638 */       params.add(Integer.valueOf(2));
/* 3639 */       params.add("%" + email + "%");
/*      */     }
/*      */ 
/* 3643 */     if ((qq != null) && (qq.trim().length() > 0)) {
/* 3644 */       sql.append(" AND A.CustomerId IN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE CompanyId = ? AND Type = ? AND Info1 like ? )");
/* 3645 */       params.add(Integer.valueOf(companyId));
/* 3646 */       params.add(Integer.valueOf(3));
/* 3647 */       params.add("%" + qq + "%");
/*      */     }
/* 3649 */     sql.append(" ORDER BY A.lastContactDate DESC, A.CreateDate DESC LIMIT 0, 5");
/*      */ 
/* 3651 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 3653 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 3654 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 3656 */       logger.warn("search customers failed", e);
/* 3657 */       throw new DBException();
/*      */     } finally {
/*      */       try {
/* 3660 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 3662 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getAllSelectedCustomerId(CustomerSearchCondition searchCondition, User loginUser)
/*      */     throws DBException
/*      */   {
/* 3685 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 3686 */     if (conn == null) {
/* 3687 */       return null;
/*      */     }
/*      */ 
/* 3690 */     List params = new ArrayList();
/*      */ 
/* 3692 */     List whereParams = new ArrayList();
/* 3693 */     whereParams.add(Integer.valueOf(searchCondition.companyId));
/*      */ 
/* 3696 */     List sqls = getListCommonSQL(params, whereParams, 
/* 3697 */       searchCondition, loginUser, false);
/*      */ 
/* 3699 */     StringBuffer sql = new StringBuffer(
/* 3700 */       "SELECT DISTINCT A.CustomerId FROM T_Customer_Info AS A ");
/*      */ 
/* 3702 */     sql.append((StringBuffer)sqls.get(0))
/* 3703 */       .append(" WHERE A.DeleteFlag = 0 AND A.CompanyId = ? ")
/* 3704 */       .append((StringBuffer)sqls.get(1));
/*      */ 
/* 3707 */     if (searchCondition.filters.size() == 0) {
/* 3708 */       sql.append(" AND (A.Type = ? OR (A.Type = ? AND A.ParentCustomerId = -1)) ");
/* 3709 */       whereParams.add(Integer.valueOf(0));
/* 3710 */       whereParams.add(Integer.valueOf(1));
/*      */     }
/*      */ 
/* 3713 */     params.addAll(whereParams);
/*      */ 
/* 3715 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 3717 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 3718 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 3720 */       logger.warn("get getAllSelectedCustomerId failed", e);
/* 3721 */       throw new DBException();
/*      */     } finally {
/*      */       try {
/* 3724 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 3726 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCompanyName(int companyId, String term, boolean isAdmin, String loginUserId)
/*      */     throws DBException, SQLException
/*      */   {
/* 3737 */     if ((term == null) || (term.trim().length() == 0)) {
/* 3738 */       return null;
/*      */     }
/* 3740 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 3741 */     if (conn == null) {
/* 3742 */       throw new DBException();
/*      */     }
/*      */ 
/* 3745 */     String sql = "SELECT CustomerId AS id , Name as label, Name as value, Visible, PermissionsId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND Type = 0 AND Name like ? ";
/*      */ 
/* 3747 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*      */       List localList;
/* 3749 */       if (isAdmin) {
/* 3750 */         sql = sql + " ORDER BY lastContactDate DESC LIMIT 0, 10";
/* 3751 */         return (List)runner.query(conn, sql, new MyMapListHandler(), new Object[] { 
/* 3752 */           Integer.valueOf(companyId), "%" + term + "%" });
/*      */       }
/* 3754 */       sql = sql + " AND (Visible = ? OR (Visible = ? AND PermissionsId = ?)) ORDER BY lastContactDate DESC LIMIT 0, 10";
/* 3755 */       return (List)runner.query(conn, sql, new MyMapListHandler(), new Object[] { Integer.valueOf(companyId), 
/* 3756 */         "%" + term + "%", Integer.valueOf(1), 
/* 3757 */         Integer.valueOf(4), loginUserId });
/*      */     } finally {
/*      */       try {
/* 3760 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 3762 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> queryOpenAPICustomer(String companyName, String queryId, String phone, String QQ, String email, String website)
/*      */     throws DBException, SQLException
/*      */   {
/* 3775 */     List customerInfos = null;
/* 3776 */     List params = new ArrayList();
/*      */ 
/* 3778 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 3779 */     if (conn == null)
/* 3780 */       throw new DBException();
/*      */     try
/*      */     {
/* 3783 */       String sql = "SELECT DISTINCT CustomerId, Name FROM T_Customer_Info WHERE 1 > 2 ";
/* 3784 */       QueryRunner runner = new QueryRunner();
/*      */ 
/* 3787 */       if ((companyName != null) && (companyName.trim().length() != 0)) {
/* 3788 */         sql = sql + " OR Name like ? ";
/* 3789 */         params.add(companyName + "_%");
/*      */       }
/*      */ 
/* 3793 */       if ((queryId != null) && (queryId.trim().length() != 0)) {
/* 3794 */         sql = sql + " OR Name like ? ";
/* 3795 */         params.add("%_" + queryId);
/*      */       }
/*      */ 
/* 3799 */       if ((email != null) && (email.trim().length() != 0)) {
/* 3800 */         sql = sql + " OR CustomerId IN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE Type = ? AND Info1 = ? ) ";
/* 3801 */         params.add(Integer.valueOf(2));
/* 3802 */         params.add(email);
/*      */       }
/*      */ 
/* 3806 */       if ((phone != null) && (phone.trim().length() != 0)) {
/* 3807 */         sql = sql + " OR CustomerId IN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE Type = ? AND Info1 = ? ) ";
/* 3808 */         params.add(Integer.valueOf(1));
/* 3809 */         params.add(phone);
/*      */       }
/*      */ 
/* 3813 */       if ((QQ != null) && (QQ.trim().length() != 0)) {
/* 3814 */         sql = sql + " OR CustomerId IN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE Type = ? AND Info1 = ? ) ";
/* 3815 */         params.add(Integer.valueOf(3));
/* 3816 */         params.add(QQ);
/*      */       }
/*      */ 
/* 3820 */       if ((website != null) && (website.trim().length() != 0)) {
/* 3821 */         sql = sql + " OR CustomerId IN (SELECT CustomerId FROM T_Customer_Ext_Info WHERE Type = ? AND Info1 = ? ) ";
/* 3822 */         params.add(Integer.valueOf(4));
/* 3823 */         params.add(website);
/*      */       }
/* 3825 */       logger.warn("final sql is " + sql);
/* 3826 */       customerInfos = (List)runner.query(conn, sql, new MyMapListHandler(), 
/* 3827 */         params.toArray());
/*      */ 
/* 3829 */       if (customerInfos.size() == 0) {
/* 3830 */         logger.warn("Query customerIds is null, and query failed.");
/* 3831 */         return null;
/*      */       }
/*      */ 
/* 3834 */       StringBuffer customerId = new StringBuffer();
/* 3835 */       for (Object mapO : customerInfos) {
                    Map map = (Map)mapO;
/* 3836 */         if (customerId.length() == 0)
/* 3837 */           customerId.append(map.get("CustomerId"));
/*      */         else {
/* 3839 */           customerId.append(",").append(map.get("CustomerId"));
/*      */         }
/*      */       }
/*      */ 
/* 3843 */       logger.warn("Query customerIds is: " + customerId);
/*      */ 
/* 3845 */       String sqlForFields = "SELECT CustomerId, Type, Info1 FROM T_Customer_Ext_Info WHERE (Type = ? OR Type = ? OR Type = ? OR Type = ?) ";
/* 3846 */       Object paramsForFields = new ArrayList();
/* 3847 */       ((List)paramsForFields).add(Integer.valueOf(2));
/* 3848 */       ((List)paramsForFields).add(Integer.valueOf(1));
/* 3849 */       ((List)paramsForFields).add(Integer.valueOf(3));
/* 3850 */       ((List)paramsForFields).add(Integer.valueOf(4));
/*      */ 
/* 3852 */       sqlForFields = sqlForFields + " AND CustomerId IN (";
/* 3853 */       sqlForFields = sqlForFields + customerId + " )";
/* 3854 */       logger.warn("Query sqlForFields is: " + sqlForFields);
/*      */ 
/* 3856 */       List result = (List)runner.query(conn, sqlForFields, 
/* 3857 */         new MyMapListHandler(), ((List)paramsForFields).toArray());
/*      */ 
/* 3859 */       for (Object mapO : result) {
                    Map map = (Map)mapO;
/* 3860 */         Map customer = getCustomerInfo(
/* 3861 */           customerInfos, ((Integer)map.get("CustomerId")).intValue());
/* 3862 */         if (customer != null)
/*      */         {
/* 3867 */           String info = (String)map.get("Info1");
/* 3868 */           if ((info != null) && (info.length() != 0))
/*      */           {
/* 3872 */             String key = "";
/* 3873 */             int type = ((Integer)map.get("Type")).intValue();
/*      */ 
/* 3875 */             switch (type) {
/*      */             case 2:
/* 3877 */               key = "email";
/* 3878 */               break;
/*      */             case 3:
/* 3880 */               key = "im";
/* 3881 */               break;
/*      */             case 1:
/* 3883 */               key = "phone";
/* 3884 */               break;
/*      */             case 4:
/* 3886 */               key = "website";
/*      */             }
/*      */ 
/* 3889 */             if ((key != null) && (key.trim().length() != 0))
/*      */             {
/* 3894 */               Object value = customer.get(key);
/* 3895 */               if (value == null)
/* 3896 */                 customer.put(key, info);
/*      */               else
/* 3898 */                 customer.put(key, (String)value + " " + info); 
/*      */             }
/*      */           }
/*      */         }
/*      */       }
/* 3902 */       return customerInfos;
/*      */     } finally {
/*      */       try {
/* 3905 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 3907 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   private Map<String, Object> getCustomerInfo(List<Map<String, Object>> customerInfos, int customerId)
/*      */   {
/* 3921 */     for (Map customer : customerInfos) {
/* 3922 */       if (customerId == ((Integer)customer.get("CustomerId")).intValue()) {
/* 3923 */         return customer;
/*      */       }
/*      */     }
/* 3926 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerName(int companyId, String term, boolean isAdmin, String loginUserId, int type, boolean isFromDeals)
/*      */   {
/* 3944 */     if ((term == null) || (term.trim().length() == 0)) {
/* 3945 */       return null;
/*      */     }
/* 3947 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 3948 */     if (conn == null) {
/* 3949 */       return null;
/*      */     }
/* 3951 */     String sql = "SELECT CustomerId AS id, Name as label, Name as value, Type FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND Name like ? ";
/* 3952 */     List params = new ArrayList();
/* 3953 */     params.add(Integer.valueOf(companyId));
/* 3954 */     params.add("%" + term + "%");
/* 3955 */     if (type >= 0) {
/* 3956 */       sql = sql + " AND Type = ? ";
/* 3957 */       params.add(Integer.valueOf(type));
/*      */     }
/* 3959 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*      */       List localList1;
/* 3962 */       if (isAdmin) {
/* 3963 */         sql = sql + " ORDER BY lastContactDate DESC LIMIT 0, 10";
/* 3964 */         return (List)runner.query(conn, sql, new MyMapListHandler(), 
/* 3965 */           params.toArray());
/*      */       }
/*      */ 
/* 3968 */       if (isFromDeals)
/*      */       {
/* 3970 */         sql = sql + " AND (Visible = ? AND PermissionsId = ?) ORDER BY lastContactDate DESC LIMIT 0, 10";
/* 3971 */         params.add(Integer.valueOf(4));
/* 3972 */         params.add(loginUserId);
/* 3973 */         return (List)runner.query(conn, sql, new MyMapListHandler(), 
/* 3974 */           params.toArray());
/*      */       }
/*      */ 
/* 3977 */       sql = sql + " AND (Visible = ? OR (Visible = ? AND PermissionsId = ?)) ORDER BY lastContactDate DESC LIMIT 0, 10";
/* 3978 */       params.add(Integer.valueOf(1));
/* 3979 */       params.add(Integer.valueOf(4));
/* 3980 */       params.add(loginUserId);
/* 3981 */       return (List)runner.query(conn, sql, new MyMapListHandler(), 
/* 3982 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 3984 */       logger.warn("get CustomerName failed", e);
/*      */     } finally {
/*      */       try {
/* 3987 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 3989 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 3992 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getContactCustomerNum(int companyId, String groupIds, int customerStatus, Date startDate, Date endDate)
/*      */   {
/* 4006 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4007 */     if (conn == null) {
/* 4008 */       return null;
/*      */     }
/* 4010 */     StringBuffer sql = new StringBuffer(
/* 4011 */       "SELECT COUNT(*) AS totalNum, PermissionsId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND Type = ? AND Visible = ? ");
/* 4012 */     List params = new ArrayList();
/* 4013 */     params.add(Integer.valueOf(companyId));
/* 4014 */     params.add(Integer.valueOf(0));
/* 4015 */     params.add(Integer.valueOf(4));
/*      */ 
/* 4019 */     if (customerStatus == 0)
/*      */     {
/* 4021 */       if (startDate != null) {
/* 4022 */         sql.append(" AND DATEDIFF(LastContactDate, ?) >= 0 ");
/* 4023 */         params.add(startDate);
/*      */       }
/* 4025 */       if (endDate != null) {
/* 4026 */         sql.append(" AND DATEDIFF(LastContactDate, ?) <= 0 ");
/* 4027 */         params.add(endDate);
/*      */       }
/* 4029 */     } else if (customerStatus == 1)
/*      */     {
/* 4031 */       if (startDate != null) {
/* 4032 */         sql.append(" AND DATEDIFF(CreateDate, ?) >= 0 ");
/* 4033 */         params.add(startDate);
/*      */       }
/* 4035 */       if (endDate != null) {
/* 4036 */         sql.append(" AND DATEDIFF(CreateDate, ?) <= 0 ");
/* 4037 */         params.add(endDate);
/*      */       }
/*      */     }
/*      */     else
/*      */     {
/* 4042 */       if (startDate != null) {
/* 4043 */         sql.append(" AND DATEDIFF(NextContactDate, ?) >= 0 ");
/* 4044 */         params.add(startDate);
/*      */       }
/* 4046 */       if (endDate != null) {
/* 4047 */         sql.append(" AND DATEDIFF(NextContactDate, ?) <= 0 ");
/* 4048 */         params.add(endDate);
/*      */       }
/*      */     }
/*      */ 
/* 4052 */     if ((groupIds != null) && (groupIds.trim().length() > 0)) {
/* 4053 */       if (!groupIds.contains(",")) {
/* 4054 */         sql.append(" AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId = ?)");
/* 4055 */         params.add(Integer.valueOf(companyId));
/* 4056 */         params.add(Integer.valueOf(Util.str2Int(groupIds)));
/*      */       } else {
/* 4058 */         sql.append(" AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 4059 */           groupIds + "))");
/* 4060 */         params.add(Integer.valueOf(companyId));
/*      */       }
/*      */     }
/*      */ 
/* 4064 */     sql.append(" GROUP BY PermissionsId ");
/* 4065 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4067 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4068 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4070 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 4073 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4075 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4078 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCreateCustomerNum(int companyId, int type, Date startDate, Date endDate)
/*      */   {
/* 4092 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4093 */     if (conn == null) {
/* 4094 */       return null;
/*      */     }
/* 4096 */     StringBuffer sql = new StringBuffer(
/* 4097 */       "SELECT COUNT(*) AS CustomerNum, PermissionsId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND Type = ? AND Visible = ? ");
/* 4098 */     List params = new ArrayList();
/* 4099 */     params.add(Integer.valueOf(companyId));
/* 4100 */     params.add(Integer.valueOf(0));
/* 4101 */     params.add(Integer.valueOf(4));
/*      */ 
/* 4103 */     if (type == 3)
/*      */     {
/* 4105 */       Calendar calendar = new GregorianCalendar();
/* 4106 */       calendar.setTime(new Date());
/* 4107 */       calendar.setFirstDayOfWeek(2);
/* 4108 */       int weekday = calendar.get(7) - 1;
/* 4109 */       if (weekday == 0) {
/* 4110 */         weekday = 7;
/*      */       }
/* 4112 */       int start = weekday - 1;
/* 4113 */       sql.append(" AND DATEDIFF(NOW(), CreateDate ) <= ? ");
/* 4114 */       params.add(Integer.valueOf(start));
/* 4115 */     } else if (type == 5)
/*      */     {
/* 4117 */       Calendar calendar = new GregorianCalendar();
/* 4118 */       calendar.setTime(new Date());
/* 4119 */       int day = calendar.get(5);
/* 4120 */       int start = day - 1;
/* 4121 */       sql.append(" AND DATEDIFF(NOW(), CreateDate) <= ? ");
/* 4122 */       params.add(Integer.valueOf(start));
/* 4123 */     } else if (type == 99)
/*      */     {
/* 4125 */       if (startDate != null) {
/* 4126 */         sql.append(" AND DATEDIFF(CreateDate, ?) >= 0 ");
/* 4127 */         params.add(startDate);
/*      */       }
/* 4129 */       if (endDate != null) {
/* 4130 */         sql.append(" AND DATEDIFF(CreateDate, ?) <= 0 ");
/* 4131 */         params.add(endDate);
/*      */       }
/*      */     }
/*      */ 
/* 4135 */     sql.append(" GROUP BY PermissionsId ");
/* 4136 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4138 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4139 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4141 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 4144 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4146 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4149 */     return null;
/*      */   }
/*      */ 
/*      */   public int getContactCustomerNum(String userId, int type)
/*      */   {
/* 4160 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4161 */     if (conn == null) {
/* 4162 */       return 0;
/*      */     }
/*      */ 
/* 4165 */     StringBuffer sql = new StringBuffer(
/* 4166 */       "SELECT COUNT(*) FROM T_Customer_Info WHERE DeleteFlag = 0 AND Type = ? AND Visible = ? AND PermissionsId = ?");
/* 4167 */     List params = new ArrayList();
/* 4168 */     params.add(Integer.valueOf(0));
/* 4169 */     params.add(Integer.valueOf(4));
/* 4170 */     params.add(userId);
/* 4171 */     if (type == 0)
/*      */     {
/* 4173 */       sql.append(" AND DATEDIFF(NOW(), LastContactDate) = 1");
/* 4174 */     } else if (type == 3)
/*      */     {
/* 4176 */       Calendar calendar = new GregorianCalendar();
/* 4177 */       calendar.setTime(new Date());
/* 4178 */       calendar.setFirstDayOfWeek(2);
/* 4179 */       int weekday = calendar.get(7) - 1;
/* 4180 */       if (weekday == 0) {
/* 4181 */         weekday = 7;
/*      */       }
/* 4183 */       int start = weekday - 1;
/* 4184 */       sql.append(" AND DATEDIFF(NOW(), LastContactDate) >= ? AND DATEDIFF(NOW(), LastContactDate) < ?");
/* 4185 */       params.add(Integer.valueOf(start));
/* 4186 */       params.add(Integer.valueOf(start + 7));
/*      */     }
/*      */ 
/* 4189 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4191 */       Object obj = runner.query(conn, sql.toString(), 
/* 4192 */         new ScalarHandler(1), params.toArray());
/* 4193 */       if (!(obj instanceof Long)) {
/* 4194 */         return 0;
/*      */       }
/* 4196 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/* 4198 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 4201 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4203 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4206 */     return 0;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCreateCustomerNumByGroupIds(int companyId, String groupIds, int step)
/*      */   {
/* 4218 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4219 */     if (conn == null) {
/* 4220 */       return null;
/*      */     }
/* 4222 */     StringBuffer sql = new StringBuffer();
/* 4223 */     sql.append("SELECT COUNT(*) AS Nums, DATEDIFF(NOW(),CreateDate) AS diffdate FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/*      */ 
/* 4225 */     List params = new ArrayList();
/* 4226 */     params.add(Integer.valueOf(companyId));
/* 4227 */     params.add(Integer.valueOf(0));
/* 4228 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4230 */     if ((groupIds != null) && (groupIds.trim().length() > 0)) {
/* 4231 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 4232 */         groupIds + ")) ");
/* 4233 */       params.add(Integer.valueOf(4));
/* 4234 */       params.add(Integer.valueOf(companyId));
/*      */     } else {
/* 4236 */       sql.append(" AND Visible = ? ");
/* 4237 */       params.add(Integer.valueOf(4));
/*      */     }
/*      */ 
/* 4241 */     if (step <= 0) {
/* 4242 */       step = 7;
/*      */     }
/* 4244 */     sql.append(" AND DATEDIFF(NOW(), CreateDate) < ? GROUP BY diffdate");
/* 4245 */     params.add(Integer.valueOf(step));
/*      */ 
/* 4247 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4249 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4250 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4252 */       logger.warn("get create customer count failed", e);
/*      */     } finally {
/*      */       try {
/* 4255 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4257 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4260 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCreateCustomerNum(int companyId, String userId, int step)
/*      */   {
/* 4272 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4273 */     if (conn == null) {
/* 4274 */       return null;
/*      */     }
/* 4276 */     StringBuffer sql = new StringBuffer();
/* 4277 */     sql.append("SELECT COUNT(*) AS Nums, DATEDIFF(NOW(),CreateDate) AS diffdate FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/*      */ 
/* 4279 */     List params = new ArrayList();
/* 4280 */     params.add(Integer.valueOf(companyId));
/* 4281 */     params.add(Integer.valueOf(0));
/* 4282 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4284 */     if ((userId != null) && (userId.trim().length() > 0)) {
/* 4285 */       sql.append(" AND Visible = ? AND PermissionsId = ? ");
/* 4286 */       params.add(Integer.valueOf(4));
/* 4287 */       params.add(userId);
/*      */     }
/*      */ 
/* 4291 */     if (step <= 0) {
/* 4292 */       step = 7;
/*      */     }
/* 4294 */     sql.append(" AND DATEDIFF(NOW(), CreateDate) < ? GROUP BY diffdate");
/* 4295 */     params.add(Integer.valueOf(step));
/*      */ 
/* 4297 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4299 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4300 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4302 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 4305 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4307 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4310 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getLastContactCustomerNumByGroupIds(int companyId, String groupIds, int step)
/*      */   {
/* 4322 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4323 */     if (conn == null) {
/* 4324 */       return null;
/*      */     }
/* 4326 */     StringBuffer sql = new StringBuffer();
/* 4327 */     sql.append("SELECT COUNT(*) AS Nums, DATEDIFF(NOW(),LastContactDate) AS diffdate FROM T_Customer_Info WHERE DeleteFlag = 0 AND LastContactDate IS NOT NULL AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/*      */ 
/* 4329 */     List params = new ArrayList();
/* 4330 */     params.add(Integer.valueOf(companyId));
/* 4331 */     params.add(Integer.valueOf(0));
/* 4332 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4334 */     if ((groupIds != null) && (groupIds.trim().length() > 0)) {
/* 4335 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 4336 */         groupIds + ")) ");
/* 4337 */       params.add(Integer.valueOf(4));
/* 4338 */       params.add(Integer.valueOf(companyId));
/*      */     } else {
/* 4340 */       sql.append(" AND Visible = ? ");
/* 4341 */       params.add(Integer.valueOf(4));
/*      */     }
/*      */ 
/* 4345 */     if (step <= 0) {
/* 4346 */       step = 7;
/*      */     }
/* 4348 */     sql.append(" AND DATEDIFF(NOW(), LastContactDate) < ? GROUP BY diffdate");
/* 4349 */     params.add(Integer.valueOf(step));
/*      */ 
/* 4351 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4353 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4354 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4356 */       logger.warn("get last contact customer count failed", e);
/*      */     } finally {
/*      */       try {
/* 4359 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4361 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4364 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getLastContactCustomerNum(int companyId, String userId, int step)
/*      */   {
/* 4377 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4378 */     if (conn == null) {
/* 4379 */       return null;
/*      */     }
/* 4381 */     StringBuffer sql = new StringBuffer();
/* 4382 */     sql.append("SELECT COUNT(*) AS Nums, DATEDIFF(NOW(),LastContactDate) AS diffdate FROM T_Customer_Info WHERE DeleteFlag = 0 AND LastContactDate IS NOT NULL AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/*      */ 
/* 4384 */     List params = new ArrayList();
/* 4385 */     params.add(Integer.valueOf(companyId));
/* 4386 */     params.add(Integer.valueOf(0));
/* 4387 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4389 */     if ((userId != null) && (userId.trim().length() > 0)) {
/* 4390 */       sql.append(" AND Visible = ? AND PermissionsId = ? ");
/* 4391 */       params.add(Integer.valueOf(4));
/* 4392 */       params.add(userId);
/*      */     }
/*      */ 
/* 4396 */     if (step <= 0) {
/* 4397 */       step = 7;
/*      */     }
/* 4399 */     sql.append(" AND DATEDIFF(NOW(), LastContactDate) < ? GROUP BY diffdate");
/* 4400 */     params.add(Integer.valueOf(step));
/*      */ 
/* 4402 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4404 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4405 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4407 */       logger.warn("get last contact count failed", e);
/*      */     } finally {
/*      */       try {
/* 4410 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4412 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4415 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getLastContactCustomer(String userId)
/*      */     throws DBException, SQLException
/*      */   {
/* 4428 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4429 */     if (conn == null) {
/* 4430 */       throw new DBException();
/*      */     }
/*      */ 
/* 4433 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4435 */       return (List)runner.query(conn, "SELECT CustomerId,Name FROM T_Customer_Info WHERE DeleteFlag = 0 AND LastContactDate is not null AND Type = ? AND Visible = ? AND PermissionsId = ? ORDER BY LastContactDate DESC LIMIT 0, 10", 
/* 4436 */         new MyMapListHandler(), new Object[] { Integer.valueOf(0), 
/* 4437 */         Integer.valueOf(4), userId });
/*      */     } finally {
/*      */       try {
/* 4440 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4442 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getAllCustomerCategoryNum(int companyId)
/*      */   {
/* 4454 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4455 */     if (conn == null) {
/* 4456 */       return null;
/*      */     }
/* 4458 */     StringBuffer sql = new StringBuffer(
/* 4459 */       "SELECT COUNT(*) AS totalNum, CategoryId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/* 4460 */     List params = new ArrayList();
/* 4461 */     params.add(Integer.valueOf(companyId));
/* 4462 */     params.add(Integer.valueOf(0));
/* 4463 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4465 */     sql.append(" GROUP BY CategoryId ");
/*      */ 
/* 4469 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4471 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4472 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4474 */       logger.warn("getCustomerCategoryNum failed", e);
/*      */     } finally {
/*      */       try {
/* 4477 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4479 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4482 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerCategoryNum(int companyId)
/*      */   {
/* 4492 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4493 */     if (conn == null) {
/* 4494 */       return null;
/*      */     }
/* 4496 */     StringBuffer sql = new StringBuffer(
/* 4497 */       "SELECT COUNT(*) AS totalNum, CategoryId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/* 4498 */     List params = new ArrayList();
/* 4499 */     params.add(Integer.valueOf(companyId));
/* 4500 */     params.add(Integer.valueOf(0));
/* 4501 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4503 */     sql.append(" AND Visible = ? ");
/* 4504 */     params.add(Integer.valueOf(4));
/*      */ 
/* 4506 */     sql.append(" GROUP BY CategoryId ");
/*      */ 
/* 4508 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4510 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4511 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4513 */       logger.warn("getCustomerCategoryNum failed", e);
/*      */     } finally {
/*      */       try {
/* 4516 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4518 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4521 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerCategoryNumByGroupIds(int companyId, String groupIds)
/*      */   {
/* 4534 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4535 */     if (conn == null) {
/* 4536 */       return null;
/*      */     }
/* 4538 */     StringBuffer sql = new StringBuffer(
/* 4539 */       "SELECT COUNT(*) AS totalNum, CategoryId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/* 4540 */     List params = new ArrayList();
/* 4541 */     params.add(Integer.valueOf(companyId));
/* 4542 */     params.add(Integer.valueOf(0));
/* 4543 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4545 */     if (!groupIds.contains(",")) {
/* 4546 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId = ? ) ");
/* 4547 */       params.add(Integer.valueOf(4));
/* 4548 */       params.add(Integer.valueOf(companyId));
/* 4549 */       params.add(Integer.valueOf(Util.str2Int(groupIds)));
/*      */     } else {
/* 4551 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 4552 */         groupIds + ")) ");
/* 4553 */       params.add(Integer.valueOf(4));
/* 4554 */       params.add(Integer.valueOf(companyId));
/*      */     }
/* 4556 */     sql.append(" GROUP BY CategoryId ");
/*      */ 
/* 4560 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4562 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4563 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4565 */       logger.warn("getCustomerCategoryNum failed", e);
/*      */     } finally {
/*      */       try {
/* 4568 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4570 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4573 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerCategoryNumByUserId(int companyId, String userId)
/*      */   {
/* 4586 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4587 */     if (conn == null) {
/* 4588 */       return null;
/*      */     }
/* 4590 */     StringBuffer sql = new StringBuffer(
/* 4591 */       "SELECT COUNT(*) AS totalNum, CategoryId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/* 4592 */     List params = new ArrayList();
/* 4593 */     params.add(Integer.valueOf(companyId));
/* 4594 */     params.add(Integer.valueOf(0));
/* 4595 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4597 */     sql.append(" AND Visible = ? ");
/* 4598 */     params.add(Integer.valueOf(4));
/*      */ 
/* 4600 */     if ((userId != null) && (userId.trim().length() > 0)) {
/* 4601 */       sql.append(" AND PermissionsId = ? ");
/* 4602 */       params.add(userId);
/*      */     }
/* 4604 */     sql.append(" GROUP BY CategoryId ");
/*      */ 
/* 4606 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4608 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4609 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4611 */       logger.warn("getCustomerCategoryNum failed", e);
/*      */     } finally {
/*      */       try {
/* 4614 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4616 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4619 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getAllCustomerCategoryChangeNum(int companyId, Date startDate, Date endDate)
/*      */   {
/* 4632 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4633 */     if (conn == null) {
/* 4634 */       return null;
/*      */     }
/* 4636 */     StringBuffer sql = new StringBuffer(
/* 4637 */       "SELECT COUNT(DISTINCT(A.CustomerId)) AS totalNum, A.CategoryId FROM T_Customer_Category_Change_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId AND A.CategoryId = B.CategoryId WHERE B.DeleteFlag = 0 AND B.CompanyId = ? AND (B.Type = ? OR (B.Type = ? AND ParentCustomerId = -1))");
/* 4638 */     List params = new ArrayList();
/* 4639 */     params.add(Integer.valueOf(companyId));
/* 4640 */     params.add(Integer.valueOf(0));
/* 4641 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4643 */     if (startDate != null) {
/* 4644 */       sql.append(" AND DATEDIFF(A.ChangeDate, ?) >= 0 ");
/* 4645 */       params.add(startDate);
/*      */     }
/* 4647 */     if (endDate != null) {
/* 4648 */       sql.append(" AND DATEDIFF(A.ChangeDate, ?) <= 0 ");
/* 4649 */       params.add(endDate);
/*      */     }
/*      */ 
/* 4652 */     sql.append(" GROUP BY A.CategoryId ");
/*      */ 
/* 4654 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4656 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4657 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4659 */       logger.warn("get Customer Category Num failed", e);
/*      */     } finally {
/*      */       try {
/* 4662 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4664 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4667 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerCategoryChangeNum(int companyId, Date startDate, Date endDate)
/*      */   {
/* 4680 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4681 */     if (conn == null) {
/* 4682 */       return null;
/*      */     }
/* 4684 */     StringBuffer sql = new StringBuffer(
/* 4685 */       "SELECT COUNT(DISTINCT(A.CustomerId)) AS totalNum, A.CategoryId FROM T_Customer_Category_Change_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId AND A.CategoryId = B.CategoryId WHERE B.DeleteFlag = 0 AND B.CompanyId = ? AND (B.Type = ? OR (B.Type = ? AND ParentCustomerId = -1))");
/* 4686 */     List params = new ArrayList();
/* 4687 */     params.add(Integer.valueOf(companyId));
/* 4688 */     params.add(Integer.valueOf(0));
/* 4689 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4691 */     sql.append(" AND B.Visible = ? ");
/* 4692 */     params.add(Integer.valueOf(4));
/*      */ 
/* 4694 */     if (startDate != null) {
/* 4695 */       sql.append(" AND DATEDIFF(A.ChangeDate, ?) >= 0 ");
/* 4696 */       params.add(startDate);
/*      */     }
/* 4698 */     if (endDate != null) {
/* 4699 */       sql.append(" AND DATEDIFF(A.ChangeDate, ?) <= 0 ");
/* 4700 */       params.add(endDate);
/*      */     }
/*      */ 
/* 4703 */     sql.append(" GROUP BY A.CategoryId ");
/*      */ 
/* 4705 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4707 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4708 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4710 */       logger.warn("get Customer Category Num failed", e);
/*      */     } finally {
/*      */       try {
/* 4713 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4715 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4718 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerCategoryChangeNumByGroupIds(int companyId, String groupIds, Date startDate, Date endDate)
/*      */   {
/* 4732 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4733 */     if (conn == null) {
/* 4734 */       return null;
/*      */     }
/* 4736 */     StringBuffer sql = new StringBuffer(
/* 4737 */       "SELECT COUNT(DISTINCT(A.CustomerId)) AS totalNum, A.CategoryId FROM T_Customer_Category_Change_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId AND A.CategoryId = B.CategoryId WHERE B.DeleteFlag = 0 AND B.CompanyId = ? AND (B.Type = ? OR (B.Type = ? AND ParentCustomerId = -1))");
/* 4738 */     List params = new ArrayList();
/* 4739 */     params.add(Integer.valueOf(companyId));
/* 4740 */     params.add(Integer.valueOf(0));
/* 4741 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4743 */     if (!groupIds.contains(",")) {
/* 4744 */       sql.append(" AND B.Visible = ? AND B.PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId = ? ) ");
/* 4745 */       params.add(Integer.valueOf(4));
/* 4746 */       params.add(Integer.valueOf(companyId));
/* 4747 */       params.add(Integer.valueOf(Util.str2Int(groupIds)));
/*      */     } else {
/* 4749 */       sql.append(" AND B.Visible = ? AND B.PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 4750 */         groupIds + ") ) ");
/* 4751 */       params.add(Integer.valueOf(4));
/* 4752 */       params.add(Integer.valueOf(companyId));
/*      */     }
/*      */ 
/* 4755 */     if (startDate != null) {
/* 4756 */       sql.append(" AND DATEDIFF(A.ChangeDate, ?) >= 0 ");
/* 4757 */       params.add(startDate);
/*      */     }
/* 4759 */     if (endDate != null) {
/* 4760 */       sql.append(" AND DATEDIFF(A.ChangeDate, ?) <= 0 ");
/* 4761 */       params.add(endDate);
/*      */     }
/*      */ 
/* 4764 */     sql.append(" GROUP BY A.CategoryId ");
/*      */ 
/* 4766 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4768 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4769 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4771 */       logger.warn("get Customer Category Num failed", e);
/*      */     } finally {
/*      */       try {
/* 4774 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4776 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4779 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerCategoryChangeNumByUserId(int companyId, String userId, Date startDate, Date endDate)
/*      */   {
/* 4793 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4794 */     if (conn == null) {
/* 4795 */       return null;
/*      */     }
/* 4797 */     StringBuffer sql = new StringBuffer(
/* 4798 */       "SELECT COUNT(DISTINCT(A.CustomerId)) AS totalNum, A.CategoryId FROM T_Customer_Category_Change_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId AND A.CategoryId = B.CategoryId WHERE B.DeleteFlag = 0 AND B.CompanyId = ? AND (B.Type = ? OR (B.Type = ? AND ParentCustomerId = -1))");
/* 4799 */     List params = new ArrayList();
/* 4800 */     params.add(Integer.valueOf(companyId));
/* 4801 */     params.add(Integer.valueOf(0));
/* 4802 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4804 */     sql.append(" AND B.Visible = ? ");
/* 4805 */     params.add(Integer.valueOf(4));
/*      */ 
/* 4807 */     if ((userId != null) && (userId.trim().length() > 0)) {
/* 4808 */       sql.append(" AND B.PermissionsId = ? ");
/* 4809 */       params.add(userId);
/*      */     }
/*      */ 
/* 4812 */     if (startDate != null) {
/* 4813 */       sql.append(" AND DATEDIFF(A.ChangeDate, ?) >= 0 ");
/* 4814 */       params.add(startDate);
/*      */     }
/* 4816 */     if (endDate != null) {
/* 4817 */       sql.append(" AND DATEDIFF(A.ChangeDate, ?) <= 0 ");
/* 4818 */       params.add(endDate);
/*      */     }
/*      */ 
/* 4821 */     sql.append(" GROUP BY A.CategoryId ");
/*      */ 
/* 4823 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4825 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4826 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4828 */       logger.warn("get Customer Category Num failed", e);
/*      */     } finally {
/*      */       try {
/* 4831 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4833 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4836 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerCategoryChangeNumByGroupIds(int companyId, String groupIds, int categoryId, Date startDate, Date endDate)
/*      */   {
/* 4851 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4852 */     if (conn == null) {
/* 4853 */       return null;
/*      */     }
/* 4855 */     StringBuffer sql = new StringBuffer(
/* 4856 */       "SELECT COUNT(DISTINCT(A.CustomerId)) AS totalNum, B.PermissionsId FROM T_Customer_Category_Change_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId AND A.CategoryId = B.CategoryId WHERE B.DeleteFlag = 0 AND B.CompanyId = ? AND (B.Type = ? OR (B.Type = ? AND ParentCustomerId = -1))");
/* 4857 */     List params = new ArrayList();
/* 4858 */     params.add(Integer.valueOf(companyId));
/* 4859 */     params.add(Integer.valueOf(0));
/* 4860 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4862 */     if ((groupIds != null) && (groupIds.trim().length() > 0)) {
/* 4863 */       if (!groupIds.contains(",")) {
/* 4864 */         sql.append(" AND B.Visible = ? AND B.PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId = ? ) ");
/* 4865 */         params.add(Integer.valueOf(4));
/* 4866 */         params.add(Integer.valueOf(companyId));
/* 4867 */         params.add(Integer.valueOf(Util.str2Int(groupIds)));
/*      */       } else {
/* 4869 */         sql.append(" AND B.Visible = ? AND B.PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 4870 */           groupIds + ")) ");
/* 4871 */         params.add(Integer.valueOf(4));
/* 4872 */         params.add(Integer.valueOf(companyId));
/*      */       }
/*      */     }
/*      */ 
/* 4876 */     if (categoryId > 0) {
/* 4877 */       sql.append(" AND A.CategoryId = ? ");
/* 4878 */       params.add(Integer.valueOf(categoryId));
/*      */     }
/*      */ 
/* 4881 */     if (startDate != null) {
/* 4882 */       sql.append(" AND DATEDIFF(A.ChangeDate, ?) >= 0 ");
/* 4883 */       params.add(startDate);
/*      */     }
/* 4885 */     if (endDate != null) {
/* 4886 */       sql.append(" AND DATEDIFF(A.ChangeDate, ?) <= 0 ");
/* 4887 */       params.add(endDate);
/*      */     }
/*      */ 
/* 4890 */     sql.append(" GROUP BY B.PermissionsId ");
/*      */ 
/* 4892 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4894 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4895 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4897 */       logger.warn("get Customer Category Num failed", e);
/*      */     } finally {
/*      */       try {
/* 4900 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4902 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4905 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerTagNumByGroupIds(int companyId, String groupIds)
/*      */   {
/* 4918 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4919 */     if (conn == null) {
/* 4920 */       return null;
/*      */     }
/* 4922 */     StringBuffer sql = new StringBuffer("SELECT COUNT(*) AS totalNum, A.TagId FROM T_Customer_Tag_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE B.DeleteFlag = 0 AND B.CompanyId = ? AND (B.Type = ? OR (B.Type = ? AND B.ParentCustomerId = -1))");
/* 4923 */     List params = new ArrayList();
/* 4924 */     params.add(Integer.valueOf(companyId));
/* 4925 */     params.add(Integer.valueOf(0));
/* 4926 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4928 */     if (!groupIds.contains(","))
/*      */     {
/* 4930 */       sql.append(" AND B.Visible = ? AND B.PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId = ? ) ");
/* 4931 */       params.add(Integer.valueOf(4));
/* 4932 */       params.add(Integer.valueOf(companyId));
/* 4933 */       params.add(Integer.valueOf(Util.str2Int(groupIds)));
/*      */     } else {
/* 4935 */       sql.append(" AND B.Visible = ? AND B.PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 4936 */         groupIds + ")) ");
/* 4937 */       params.add(Integer.valueOf(4));
/* 4938 */       params.add(Integer.valueOf(companyId));
/*      */     }
/*      */ 
/* 4941 */     sql.append(" GROUP BY A.TagId ");
/*      */ 
/* 4943 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4945 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4946 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4948 */       logger.warn("get Customer tag failed", e);
/*      */     } finally {
/*      */       try {
/* 4951 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4953 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4956 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerTagNum(int companyId)
/*      */   {
/* 4966 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 4967 */     if (conn == null) {
/* 4968 */       return null;
/*      */     }
/* 4970 */     StringBuffer sql = new StringBuffer("SELECT COUNT(*) AS totalNum, A.TagId FROM T_Customer_Tag_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE B.DeleteFlag = 0 AND B.CompanyId = ? AND (B.Type = ? OR (B.Type = ? AND B.ParentCustomerId = -1))");
/* 4971 */     List params = new ArrayList();
/* 4972 */     params.add(Integer.valueOf(companyId));
/* 4973 */     params.add(Integer.valueOf(0));
/* 4974 */     params.add(Integer.valueOf(1));
/*      */ 
/* 4976 */     sql.append(" AND B.Visible = ? ");
/* 4977 */     params.add(Integer.valueOf(4));
/*      */ 
/* 4979 */     sql.append(" GROUP BY A.TagId ");
/*      */ 
/* 4981 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 4983 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 4984 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 4986 */       logger.warn("get Customer tag failed", e);
/*      */     } finally {
/*      */       try {
/* 4989 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 4991 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 4994 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getAllCustomerTagNum(int companyId)
/*      */   {
/* 5004 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5005 */     if (conn == null) {
/* 5006 */       return null;
/*      */     }
/* 5008 */     StringBuffer sql = new StringBuffer("SELECT COUNT(*) AS totalNum, A.TagId FROM T_Customer_Tag_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE B.DeleteFlag = 0 AND B.CompanyId = ? AND (B.Type = ? OR (B.Type = ? AND B.ParentCustomerId = -1))");
/* 5009 */     List params = new ArrayList();
/* 5010 */     params.add(Integer.valueOf(companyId));
/* 5011 */     params.add(Integer.valueOf(0));
/* 5012 */     params.add(Integer.valueOf(1));
/* 5013 */     sql.append(" GROUP BY A.TagId ");
/*      */ 
/* 5015 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5017 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 5018 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 5020 */       logger.warn("get Customer tag failed", e);
/*      */     } finally {
/*      */       try {
/* 5023 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5025 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 5028 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerTagNumByUserId(int companyId, String userId)
/*      */   {
/* 5040 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5041 */     if (conn == null) {
/* 5042 */       return null;
/*      */     }
/* 5044 */     StringBuffer sql = new StringBuffer("SELECT COUNT(*) AS totalNum, A.TagId FROM T_Customer_Tag_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE B.DeleteFlag = 0 AND B.CompanyId = ? AND (B.Type = ? OR (B.Type = ? AND B.ParentCustomerId = -1))");
/* 5045 */     List params = new ArrayList();
/* 5046 */     params.add(Integer.valueOf(companyId));
/* 5047 */     params.add(Integer.valueOf(0));
/* 5048 */     params.add(Integer.valueOf(1));
/*      */ 
/* 5050 */     sql.append(" AND B.Visible = ? ");
/* 5051 */     params.add(Integer.valueOf(4));
/* 5052 */     if ((userId != null) && (userId.trim().length() > 0)) {
/* 5053 */       sql.append("  AND B.PermissionsId = ? ");
/* 5054 */       params.add(userId);
/*      */     }
/*      */ 
/* 5057 */     sql.append(" GROUP BY A.TagId ");
/*      */ 
/* 5059 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5061 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 5062 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 5064 */       logger.warn("get Customer tag failed", e);
/*      */     } finally {
/*      */       try {
/* 5067 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5069 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 5072 */     return null;
/*      */   }
/*      */ 
/*      */   public int getNotContactedCustomerNumByGroupIds(int companyId, String groupIds)
/*      */   {
/* 5084 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5085 */     if (conn == null) {
/* 5086 */       return 0;
/*      */     }
/* 5088 */     StringBuffer sql = new StringBuffer(
/* 5089 */       "SELECT COUNT(*) AS totalNum FROM T_Customer_Info WHERE DeleteFlag = 0 AND LastContactDate is null AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/* 5090 */     List params = new ArrayList();
/* 5091 */     params.add(Integer.valueOf(companyId));
/* 5092 */     params.add(Integer.valueOf(0));
/* 5093 */     params.add(Integer.valueOf(1));
/*      */ 
/* 5095 */     if ((groupIds == null) || (groupIds.trim().length() == 0))
/*      */     {
/* 5097 */       sql.append(" AND Visible = ? ");
/* 5098 */       params.add(Integer.valueOf(4));
/* 5099 */     } else if (!groupIds.contains(",")) {
/* 5100 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId = ? ) ");
/* 5101 */       params.add(Integer.valueOf(4));
/* 5102 */       params.add(Integer.valueOf(companyId));
/* 5103 */       params.add(Integer.valueOf(Util.str2Int(groupIds)));
/*      */     } else {
/* 5105 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 5106 */         groupIds + ")) ");
/* 5107 */       params.add(Integer.valueOf(4));
/* 5108 */       params.add(Integer.valueOf(companyId));
/*      */     }
/*      */ 
/* 5111 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5113 */       Object obj = runner.query(conn, sql.toString(), 
/* 5114 */         new ScalarHandler(1), params.toArray());
/* 5115 */       if (!(obj instanceof Long)) {
/* 5116 */         return 0;
/*      */       }
/* 5118 */       return ((Long)obj).intValue();
/*      */     }
/*      */     catch (SQLException e) {
/* 5121 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 5124 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5126 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/* 5130 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getNotContactedCustomerNumByUserId(int companyId, String userId)
/*      */   {
/* 5141 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5142 */     if (conn == null) {
/* 5143 */       return 0;
/*      */     }
/* 5145 */     StringBuffer sql = new StringBuffer(
/* 5146 */       "SELECT COUNT(*) AS totalNum FROM T_Customer_Info WHERE DeleteFlag = 0 AND LastContactDate is null AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/* 5147 */     List params = new ArrayList();
/* 5148 */     params.add(Integer.valueOf(companyId));
/* 5149 */     params.add(Integer.valueOf(0));
/* 5150 */     params.add(Integer.valueOf(1));
/*      */ 
/* 5152 */     if ((userId != null) && (userId.trim().length() > 0)) {
/* 5153 */       sql.append(" AND Visible = ? AND PermissionsId = ? ");
/* 5154 */       params.add(Integer.valueOf(4));
/* 5155 */       params.add(userId);
/*      */     }
/*      */ 
/* 5158 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5160 */       Object obj = runner.query(conn, sql.toString(), 
/* 5161 */         new ScalarHandler(1), params.toArray());
/* 5162 */       if (!(obj instanceof Long)) {
/* 5163 */         return 0;
/*      */       }
/* 5165 */       return ((Long)obj).intValue();
/*      */     }
/*      */     catch (SQLException e) {
/* 5168 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 5171 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5173 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/* 5177 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getTotalCustomerNumByGroupIds(int companyId, String groupIds)
/*      */   {
/* 5188 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5189 */     if (conn == null) {
/* 5190 */       return 0;
/*      */     }
/* 5192 */     StringBuffer sql = new StringBuffer("SELECT COUNT(*) AS totalNum FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/* 5193 */     List params = new ArrayList();
/* 5194 */     params.add(Integer.valueOf(companyId));
/* 5195 */     params.add(Integer.valueOf(0));
/* 5196 */     params.add(Integer.valueOf(1));
/*      */ 
/* 5198 */     if ((groupIds == null) || (groupIds.trim().length() == 0))
/*      */     {
/* 5200 */       sql.append(" AND Visible = ? ");
/* 5201 */       params.add(Integer.valueOf(4));
/* 5202 */     } else if (!groupIds.contains(",")) {
/* 5203 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId = ?)");
/* 5204 */       params.add(Integer.valueOf(4));
/* 5205 */       params.add(Integer.valueOf(companyId));
/* 5206 */       params.add(Integer.valueOf(Util.str2Int(groupIds)));
/*      */     } else {
/* 5208 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 5209 */         groupIds + "))");
/* 5210 */       params.add(Integer.valueOf(4));
/* 5211 */       params.add(Integer.valueOf(companyId));
/*      */     }
/*      */ 
/* 5214 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5216 */       Object obj = runner.query(conn, sql.toString(), 
/* 5217 */         new ScalarHandler(1), params.toArray());
/* 5218 */       if (!(obj instanceof Long)) {
/* 5219 */         return 0;
/*      */       }
/* 5221 */       return ((Long)obj).intValue();
/*      */     }
/*      */     catch (SQLException e) {
/* 5224 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 5227 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5229 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 5232 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getTotalCustomerNumByUserId(int companyId, String userId)
/*      */   {
/* 5243 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5244 */     if (conn == null) {
/* 5245 */       return 0;
/*      */     }
/* 5247 */     StringBuffer sql = new StringBuffer("SELECT COUNT(*) AS totalNum FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/* 5248 */     List params = new ArrayList();
/* 5249 */     params.add(Integer.valueOf(companyId));
/* 5250 */     params.add(Integer.valueOf(0));
/* 5251 */     params.add(Integer.valueOf(1));
/*      */ 
/* 5254 */     if ((userId != null) && (userId.trim().length() > 0)) {
/* 5255 */       sql.append(" AND Visible = ? AND PermissionsId = ? ");
/* 5256 */       params.add(Integer.valueOf(4));
/* 5257 */       params.add(userId);
/*      */     }
/*      */ 
/* 5260 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5262 */       Object obj = runner.query(conn, sql.toString(), 
/* 5263 */         new ScalarHandler(1), params.toArray());
/* 5264 */       if (!(obj instanceof Long)) {
/* 5265 */         return 0;
/*      */       }
/* 5267 */       return ((Long)obj).intValue();
/*      */     }
/*      */     catch (SQLException e) {
/* 5270 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 5273 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5275 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 5278 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getTotalContactedCustomerNumByGroupIds(int companyId, String groupIds)
/*      */   {
/* 5290 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5291 */     if (conn == null) {
/* 5292 */       return 0;
/*      */     }
/* 5294 */     StringBuffer sql = new StringBuffer("SELECT COUNT(*) AS totalNum FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/* 5295 */     List params = new ArrayList();
/* 5296 */     params.add(Integer.valueOf(companyId));
/* 5297 */     params.add(Integer.valueOf(0));
/* 5298 */     params.add(Integer.valueOf(1));
/*      */ 
/* 5300 */     if ((groupIds == null) || (groupIds.trim().length() == 0))
/*      */     {
/* 5302 */       sql.append(" AND Visible = ? ");
/* 5303 */       params.add(Integer.valueOf(4));
/* 5304 */     } else if (!groupIds.contains(",")) {
/* 5305 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId = ?)");
/* 5306 */       params.add(Integer.valueOf(4));
/* 5307 */       params.add(Integer.valueOf(companyId));
/* 5308 */       params.add(Integer.valueOf(Util.str2Int(groupIds)));
/*      */     } else {
/* 5310 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 5311 */         groupIds + "))");
/* 5312 */       params.add(Integer.valueOf(4));
/* 5313 */       params.add(Integer.valueOf(companyId));
/*      */     }
/*      */ 
/* 5316 */     sql.append(" AND LastContactDate IS NOT NULL ");
/* 5317 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5319 */       Object obj = runner.query(conn, sql.toString(), 
/* 5320 */         new ScalarHandler(1), params.toArray());
/* 5321 */       if (!(obj instanceof Long)) {
/* 5322 */         return 0;
/*      */       }
/* 5324 */       return ((Long)obj).intValue();
/*      */     }
/*      */     catch (SQLException e) {
/* 5327 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 5330 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5332 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/* 5336 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getTotalContactedCustomerNumByUserId(int companyId, String userId)
/*      */   {
/* 5347 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5348 */     if (conn == null) {
/* 5349 */       return 0;
/*      */     }
/* 5351 */     StringBuffer sql = new StringBuffer("SELECT COUNT(*) AS totalNum FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/* 5352 */     List params = new ArrayList();
/* 5353 */     params.add(Integer.valueOf(companyId));
/* 5354 */     params.add(Integer.valueOf(0));
/* 5355 */     params.add(Integer.valueOf(1));
/*      */ 
/* 5358 */     if ((userId != null) && (userId.trim().length() > 0)) {
/* 5359 */       sql.append(" AND Visible = ? AND PermissionsId = ? ");
/* 5360 */       params.add(Integer.valueOf(4));
/* 5361 */       params.add(userId);
/*      */     }
/* 5363 */     sql.append(" AND LastContactDate IS NOT NULL ");
/* 5364 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5366 */       Object obj = runner.query(conn, sql.toString(), 
/* 5367 */         new ScalarHandler(1), params.toArray());
/* 5368 */       if (!(obj instanceof Long)) {
/* 5369 */         return 0;
/*      */       }
/* 5371 */       return ((Long)obj).intValue();
/*      */     }
/*      */     catch (SQLException e) {
/* 5374 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 5377 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5379 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/* 5383 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getContactedCustomerNumByGroupIds(int companyId, String groupIds, Date startDate, Date endDate)
/*      */   {
/* 5397 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5398 */     if (conn == null) {
/* 5399 */       return 0;
/*      */     }
/* 5401 */     StringBuffer sql = new StringBuffer(
/* 5402 */       "SELECT COUNT(*) AS totalNum FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/* 5403 */     List params = new ArrayList();
/* 5404 */     params.add(Integer.valueOf(companyId));
/* 5405 */     params.add(Integer.valueOf(0));
/* 5406 */     params.add(Integer.valueOf(1));
/*      */ 
/* 5408 */     if (!groupIds.contains(",")) {
/* 5409 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId = ?)");
/* 5410 */       params.add(Integer.valueOf(4));
/* 5411 */       params.add(Integer.valueOf(companyId));
/* 5412 */       params.add(Integer.valueOf(Util.str2Int(groupIds)));
/*      */     } else {
/* 5414 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 5415 */         groupIds + "))");
/* 5416 */       params.add(Integer.valueOf(4));
/* 5417 */       params.add(Integer.valueOf(companyId));
/*      */     }
/*      */ 
/* 5420 */     if ((startDate == null) && (endDate == null))
/*      */     {
/* 5422 */       sql.append(" AND DATEDIFF(LastContactDate, NOW()) = 0");
/*      */     }
/*      */     else {
/* 5425 */       if (startDate != null) {
/* 5426 */         sql.append(" AND DATEDIFF(LastContactDate, ?) >= 0 ");
/* 5427 */         params.add(startDate);
/*      */       }
/* 5429 */       if (endDate != null) {
/* 5430 */         sql.append(" AND DATEDIFF(LastContactDate, ?) <= 0 ");
/* 5431 */         params.add(endDate);
/*      */       }
/*      */     }
/*      */ 
/* 5435 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5437 */       Object obj = runner.query(conn, sql.toString(), 
/* 5438 */         new ScalarHandler(1), params.toArray());
/* 5439 */       if (!(obj instanceof Long)) {
/* 5440 */         return 0;
/*      */       }
/* 5442 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/* 5444 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 5447 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5449 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/* 5453 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getContactedCustomerNumByUserId(int companyId, String userId, Date startDate, Date endDate)
/*      */   {
/* 5467 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5468 */     if (conn == null) {
/* 5469 */       return 0;
/*      */     }
/* 5471 */     StringBuffer sql = new StringBuffer(
/* 5472 */       "SELECT COUNT(*) AS totalNum FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND (Type = ? OR (Type = ? AND ParentCustomerId = -1))");
/* 5473 */     List params = new ArrayList();
/* 5474 */     params.add(Integer.valueOf(companyId));
/* 5475 */     params.add(Integer.valueOf(0));
/* 5476 */     params.add(Integer.valueOf(1));
/*      */ 
/* 5478 */     if ((userId != null) && (userId.trim().length() > 0)) {
/* 5479 */       sql.append(" AND Visible = ? AND PermissionsId = ? ");
/* 5480 */       params.add(Integer.valueOf(4));
/* 5481 */       params.add(userId);
/*      */     }
/*      */ 
/* 5484 */     if ((startDate == null) && (endDate == null))
/*      */     {
/* 5486 */       sql.append(" AND DATEDIFF(LastContactDate, NOW()) = 0");
/*      */     }
/*      */     else {
/* 5489 */       if (startDate != null) {
/* 5490 */         sql.append(" AND DATEDIFF(LastContactDate, ?) >= 0 ");
/* 5491 */         params.add(startDate);
/*      */       }
/* 5493 */       if (endDate != null) {
/* 5494 */         sql.append(" AND DATEDIFF(LastContactDate, ?) <= 0 ");
/* 5495 */         params.add(endDate);
/*      */       }
/*      */     }
/*      */ 
/* 5499 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5501 */       Object obj = runner.query(conn, sql.toString(), 
/* 5502 */         new ScalarHandler(1), params.toArray());
/* 5503 */       if (!(obj instanceof Long)) {
/* 5504 */         return 0;
/*      */       }
/* 5506 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/* 5508 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 5511 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5513 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/* 5517 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getNextContactedCustomerNumByGroupIds(int companyId, String groupIds, Date startDate, Date endDate)
/*      */   {
/* 5531 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5532 */     if (conn == null) {
/* 5533 */       return 0;
/*      */     }
/* 5535 */     StringBuffer sql = new StringBuffer(
/* 5536 */       "SELECT COUNT(*) AS totalNum FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? ");
/* 5537 */     List params = new ArrayList();
/* 5538 */     params.add(Integer.valueOf(companyId));
/*      */ 
/* 5540 */     if ((groupIds == null) || (groupIds.trim().length() == 0))
/*      */     {
/* 5542 */       sql.append(" AND Visible = ? ");
/* 5543 */       params.add(Integer.valueOf(4));
/* 5544 */     } else if (!groupIds.contains(",")) {
/* 5545 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId = ? ) ");
/* 5546 */       params.add(Integer.valueOf(4));
/* 5547 */       params.add(Integer.valueOf(companyId));
/* 5548 */       params.add(Integer.valueOf(Util.str2Int(groupIds)));
/*      */     } else {
/* 5550 */       sql.append(" AND Visible = ? AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 5551 */         groupIds + ") ) ");
/* 5552 */       params.add(Integer.valueOf(4));
/* 5553 */       params.add(Integer.valueOf(companyId));
/*      */     }
/*      */ 
/* 5556 */     if ((startDate == null) && (endDate == null))
/*      */     {
/* 5558 */       sql.append(" AND DATEDIFF(NextContactDate, NOW()) = 0");
/*      */     }
/*      */     else {
/* 5561 */       if (startDate != null) {
/* 5562 */         sql.append(" AND DATEDIFF(NextContactDate, ?) >= 0 ");
/* 5563 */         params.add(startDate);
/*      */       }
/* 5565 */       if (endDate != null) {
/* 5566 */         sql.append(" AND DATEDIFF(NextContactDate, ?) <= 0 ");
/* 5567 */         params.add(endDate);
/*      */       }
/*      */     }
/*      */ 
/* 5571 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5573 */       Object obj = runner.query(conn, sql.toString(), 
/* 5574 */         new ScalarHandler(1), params.toArray());
/* 5575 */       if (!(obj instanceof Long)) {
/* 5576 */         return 0;
/*      */       }
/* 5578 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/* 5580 */       logger.warn("get next contact customer count failed", e);
/*      */     } finally {
/*      */       try {
/* 5583 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5585 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/* 5589 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getNextContactedCustomerNumByUserId(int companyId, String userId, Date startDate, Date endDate)
/*      */   {
/* 5603 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5604 */     if (conn == null) {
/* 5605 */       return 0;
/*      */     }
/* 5607 */     StringBuffer sql = new StringBuffer(
/* 5608 */       "SELECT COUNT(*) AS totalNum FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? ");
/* 5609 */     List params = new ArrayList();
/* 5610 */     params.add(Integer.valueOf(companyId));
/* 5611 */     if ((userId != null) && (userId.trim().length() > 0)) {
/* 5612 */       sql.append(" AND Visible = ? AND PermissionsId = ? ");
/* 5613 */       params.add(Integer.valueOf(4));
/* 5614 */       params.add(userId);
/*      */     }
/*      */ 
/* 5617 */     if ((startDate == null) && (endDate == null))
/*      */     {
/* 5619 */       sql.append(" AND DATEDIFF(NextContactDate, NOW()) = 0");
/*      */     }
/*      */     else {
/* 5622 */       if (startDate != null) {
/* 5623 */         sql.append(" AND DATEDIFF(NextContactDate, ?) >= 0 ");
/* 5624 */         params.add(startDate);
/*      */       }
/* 5626 */       if (endDate != null) {
/* 5627 */         sql.append(" AND DATEDIFF(NextContactDate, ?) <= 0 ");
/* 5628 */         params.add(endDate);
/*      */       }
/*      */     }
/*      */ 
/* 5632 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5634 */       Object obj = runner.query(conn, sql.toString(), 
/* 5635 */         new ScalarHandler(1), params.toArray());
/* 5636 */       if (!(obj instanceof Long)) {
/* 5637 */         return 0;
/*      */       }
/* 5639 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/* 5641 */       logger.warn("get next contact customer count failed", e);
/*      */     } finally {
/*      */       try {
/* 5644 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5646 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/* 5650 */     return 0;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerTotalNumInfoByGroupIds(int companyId, String groupIds)
/*      */   {
/* 5667 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5668 */     if (conn == null) {
/* 5669 */       return null;
/*      */     }
/* 5671 */     List params = new ArrayList();
/* 5672 */     params.add(Integer.valueOf(companyId));
/* 5673 */     params.add(Integer.valueOf(4));
/* 5674 */     params.add(Integer.valueOf(0));
/* 5675 */     params.add(Integer.valueOf(1));
/* 5676 */     params.add(Integer.valueOf(-1));
/*      */ 
/* 5678 */     StringBuffer sql = new StringBuffer();
/* 5679 */     sql.append("SELECT COUNT(*) AS totalNum, PermissionsId FROM T_Customer_Info WHERE DeleteFlag = 0 AND CompanyId = ? AND Visible = ? AND (Type = ?  OR (Type = ? AND ParentCustomerId = ?)) ");
/*      */ 
/* 5681 */     if ((groupIds != null) && (groupIds.trim().length() > 0)) {
/* 5682 */       if (!groupIds.contains(",")) {
/* 5683 */         sql.append(" AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId = ?)");
/* 5684 */         params.add(Integer.valueOf(companyId));
/* 5685 */         params.add(Integer.valueOf(Util.str2Int(groupIds)));
/*      */       } else {
/* 5687 */         sql.append(" AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE CompanyId = ? AND GroupId IN (" + 
/* 5688 */           groupIds + "))");
/* 5689 */         params.add(Integer.valueOf(companyId));
/*      */       }
/*      */     }
/*      */ 
/* 5693 */     sql.append(" GROUP BY PermissionsId ");
/* 5694 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 5697 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 5698 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 5700 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 5703 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5705 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 5708 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getNextContactCustomerTotalNumInfo(int companyId, int type, Date startDate, Date endDate)
/*      */   {
/* 5722 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5723 */     if (conn == null) {
/* 5724 */       return null;
/*      */     }
/* 5726 */     StringBuffer sql = new StringBuffer(
/* 5727 */       "SELECT count(*) AS totalNum, PermissionsId FROM T_Customer_Info WHERE DeleteFlag = 0 AND companyId = ? AND Visible = ? ");
/* 5728 */     List params = new ArrayList();
/* 5729 */     params.add(Integer.valueOf(companyId));
/* 5730 */     params.add(Integer.valueOf(4));
/*      */     int start;
/* 5731 */     if (type == 3)
/*      */     {
/* 5733 */       Calendar calendar = new GregorianCalendar();
/* 5734 */       calendar.setTime(new Date());
/* 5735 */       calendar.setFirstDayOfWeek(2);
/* 5736 */       int weekday = calendar.get(7) - 1;
/* 5737 */       if (weekday == 0) {
/* 5738 */         weekday = 7;
/*      */       }
/* 5740 */       start = weekday * -1 + 1;
/* 5741 */       sql.append(" AND DATEDIFF(NextContactDate, NOW()) >= ? AND DATEDIFF(NextContactDate, NOW()) < ? ");
/* 5742 */       params.add(Integer.valueOf(start));
/* 5743 */       params.add(Integer.valueOf(start + 7));
/* 5744 */     } else if (type == 5)
/*      */     {
/* 5746 */       Calendar calendar = new GregorianCalendar();
/* 5747 */       calendar.setTime(new Date());
/* 5748 */       int day = calendar.get(5);
/* 5749 */       int totalDays = calendar.getActualMaximum(5);
/* 5750 */       start = day * -1 + 1;
/* 5751 */       sql.append(" AND DATEDIFF(NextContactDate, NOW()) >= ? AND DATEDIFF(NextContactDate, NOW()) < ? ");
/* 5752 */       params.add(Integer.valueOf(start));
/* 5753 */       params.add(Integer.valueOf(start + totalDays));
/* 5754 */     } else if (type == 99)
/*      */     {
/* 5756 */       if (startDate != null) {
/* 5757 */         sql.append(" AND DATEDIFF(NextContactDate, ?) >= 0 ");
/* 5758 */         params.add(startDate);
/*      */       }
/* 5760 */       if (endDate != null) {
/* 5761 */         sql.append(" AND DATEDIFF(NextContactDate, ?) <= 0 ");
/* 5762 */         params.add(endDate);
/*      */       }
/*      */     }
/*      */ 
/* 5766 */     sql.append(" GROUP BY PermissionsId");
/* 5767 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5769 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 5770 */         params.toArray());
/*      */     } catch (SQLException e) {
/* 5772 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 5775 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5777 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 5780 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCompanyPeoples(String companyIds)
/*      */     throws DBException, SQLException
/*      */   {
/* 5793 */     if ((companyIds == null) || (companyIds.trim().length() == 0)) {
/* 5794 */       return null;
/*      */     }
/* 5796 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5797 */     if (conn == null) {
/* 5798 */       throw new DBException();
/*      */     }
/*      */ 
/* 5801 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5803 */       StringBuffer sql = new StringBuffer(
/* 5804 */         "SELECT CustomerId, Name, ParentCustomerId FROM T_Customer_Info WHERE DeleteFlag = 0 AND ParentCustomerId IN (");
/* 5805 */       sql.append(companyIds).append(") ORDER BY ParentCustomerId");
/* 5806 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler());
/*      */     } finally {
/*      */       try {
/* 5809 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5811 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerAllExtInfo(Connection conn, String customerIds)
/*      */     throws SQLException
/*      */   {
/* 5826 */     if ((customerIds == null) || (customerIds.trim().length() == 0)) {
/* 5827 */       return null;
/*      */     }
/*      */ 
/* 5830 */     QueryRunner runner = new QueryRunner();
/* 5831 */     StringBuffer sql = new StringBuffer("SELECT Type, Info1, CustomerId AS Id FROM T_Customer_Ext_Info WHERE CustomerId IN (");
/* 5832 */     sql.append(customerIds).append(") ORDER BY CustomerId , Type DESC");
/* 5833 */     return (List)runner.query(conn, sql.toString(), new MyMapListHandler());
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getPeopleAllExtInfo(Connection conn, String companyIds)
/*      */     throws SQLException
/*      */   {
/* 5846 */     if ((companyIds == null) || (companyIds.trim().length() == 0)) {
/* 5847 */       return null;
/*      */     }
/*      */ 
/* 5850 */     QueryRunner runner = new QueryRunner();
/*      */ 
/* 5852 */     StringBuffer sql = new StringBuffer("SELECT A.Type, A.Info1, B.ParentCustomerId AS Id FROM T_Customer_Ext_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE B.ParentCustomerId IN (");
/* 5853 */     sql.append(companyIds).append(") ORDER BY ParentCustomerId, Type DESC");
/* 5854 */     return (List)runner.query(conn, sql.toString(), new MyMapListHandler());
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getPeoplesName(Connection conn, String companyIds)
/*      */     throws SQLException
/*      */   {
/* 5867 */     if ((companyIds == null) || (companyIds.trim().length() == 0)) {
/* 5868 */       return null;
/*      */     }
/*      */ 
/* 5871 */     QueryRunner runner = new QueryRunner();
/* 5872 */     StringBuffer sql = new StringBuffer("SELECT Name, Title, ParentCustomerId AS Id FROM T_Customer_Info WHERE ParentCustomerId IN (");
/* 5873 */     sql.append(companyIds).append(") ORDER BY ParentCustomerId DESC");
/* 5874 */     return (List)runner.query(conn, sql.toString(), new MyMapListHandler());
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getLastRemarksInfo(String customerIds)
/*      */     throws DBException, SQLException
/*      */   {
/* 5887 */     if ((customerIds == null) || (customerIds.trim().length() == 0)) {
/* 5888 */       return null;
/*      */     }
/* 5890 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5891 */     if (conn == null) {
/* 5892 */       throw new DBException();
/*      */     }
/*      */ 
/* 5895 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5897 */       StringBuffer sql = new StringBuffer("SELECT B.NoteId, B.Remark AS NoteRemark, B.NoteDate, B.CreateUserId AS NoteCreateUserId FROM T_Customer_Info AS A LEFT JOIN T_Note_Info AS B ON A.LastNoteId = B.NoteId WHERE A.CustomerId IN (");
/* 5898 */       sql.append(customerIds)
/* 5899 */         .append(" ) ORDER BY CustomerId , Type DESC");
/* 5900 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler());
/*      */     } finally {
/*      */       try {
/* 5903 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5905 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getParentCustomerInfo(String customerIds)
/*      */     throws DBException, SQLException
/*      */   {
/* 5920 */     if ((customerIds == null) || (customerIds.trim().length() == 0)) {
/* 5921 */       return null;
/*      */     }
/* 5923 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5924 */     if (conn == null) {
/* 5925 */       throw new DBException();
/*      */     }
/*      */ 
/* 5928 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5930 */       StringBuffer sql = new StringBuffer("SELECT CustomerId, Name AS ParentCustomerName FROM T_Customer_Info WHERE CustomerId IN (");
/* 5931 */       sql.append(customerIds)
/* 5932 */         .append(" ) ORDER BY CustomerId , Type DESC");
/* 5933 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler());
/*      */     } finally {
/*      */       try {
/* 5936 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5938 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerExtInfo(String customerIds, String typeIds)
/*      */     throws DBException, SQLException
/*      */   {
/* 5953 */     if ((customerIds == null) || (customerIds.trim().length() == 0)) {
/* 5954 */       return null;
/*      */     }
/* 5956 */     if ((typeIds == null) || (typeIds.trim().length() == 0)) {
/* 5957 */       return null;
/*      */     }
/* 5959 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5960 */     if (conn == null) {
/* 5961 */       throw new DBException();
/*      */     }
/*      */ 
/* 5964 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5966 */       StringBuffer sql = new StringBuffer("SELECT Type, Info1, CustomerId AS Id FROM T_Customer_Ext_Info WHERE CustomerId IN (");
/* 5967 */       sql.append(customerIds).append(") AND Type IN ( ").append(typeIds)
/* 5968 */         .append(" ) ORDER BY CustomerId , Type DESC");
/* 5969 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler());
/*      */     } finally {
/*      */       try {
/* 5972 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 5974 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerPhone(String customerIds)
/*      */     throws DBException, SQLException
/*      */   {
/* 5989 */     if ((customerIds == null) || (customerIds.trim().length() == 0)) {
/* 5990 */       return null;
/*      */     }
/* 5992 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 5993 */     if (conn == null) {
/* 5994 */       throw new DBException();
/*      */     }
/*      */ 
/* 5997 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 5999 */       StringBuffer sql = new StringBuffer(
/* 6000 */         "SELECT Info1, CustomerId FROM T_Customer_Ext_Info WHERE Type = ? AND CustomerId IN (");
/* 6001 */       sql.append(customerIds).append(" ) ORDER BY CustomerId ASC");
/* 6002 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), new Object[] { 
/* 6003 */         Integer.valueOf(1) });
/*      */     } finally {
/*      */       try {
/* 6006 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6008 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int getUserCompanyNum(String userId)
/*      */   {
/* 6020 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6021 */     if (conn == null) {
/* 6022 */       return 0;
/*      */     }
/*      */ 
/* 6025 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 6027 */       Object obj = runner.query(conn, "SELECT COUNT(*) FROM T_Customer_Info WHERE DeleteFlag = 0 AND Type = ? AND Visible = ? AND PermissionsId = ?", 
/* 6028 */         new ScalarHandler(1), new Object[] { Integer.valueOf(0), 
/* 6029 */         Integer.valueOf(4), userId });
/* 6030 */       if (!(obj instanceof Long)) {
/* 6031 */         return 0;
/*      */       }
/* 6033 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/* 6035 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 6038 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6040 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 6043 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getUserPeopleNum(String userId)
/*      */   {
/* 6053 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6054 */     if (conn == null) {
/* 6055 */       return 0;
/*      */     }
/* 6057 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 6059 */       Object obj = runner.query(conn, "SELECT COUNT(*) FROM T_Customer_Info WHERE DeleteFlag = 0 AND Type = ? AND Visible = ? AND PermissionsId = ?", 
/* 6060 */         new ScalarHandler(1), new Object[] { Integer.valueOf(1), 
/* 6061 */         Integer.valueOf(4), userId });
/* 6062 */       if (!(obj instanceof Long)) {
/* 6063 */         return 0;
/*      */       }
/* 6065 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/* 6067 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/* 6070 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6072 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 6075 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getNeedContactCustomerNum(String userId, int type, Date start, Date end)
/*      */   {
/* 6086 */     if ((userId == null) || (userId.length() == 0)) {
/* 6087 */       return 0;
/*      */     }
/* 6089 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6090 */     if (conn == null) {
/* 6091 */       return 0;
/*      */     }
/* 6093 */     if ((start == null) && (end == null)) {
/* 6094 */       end = new Date();
/*      */     }
/* 6096 */     StringBuffer sql = new StringBuffer(
/* 6097 */       "SELECT COUNT(*) FROM T_Customer_Info WHERE DeleteFlag = 0 AND Visible = ? AND PermissionsId = ? ");
/*      */ 
/* 6099 */     List params = new ArrayList();
/*      */ 
/* 6101 */     params.add(Integer.valueOf(4));
/* 6102 */     params.add(userId);
/*      */ 
/* 6104 */     if (type > -1) {
/* 6105 */       sql.append(" AND Type = ? ");
/* 6106 */       params.add(Integer.valueOf(type));
/*      */     }
/*      */ 
/* 6109 */     if (start != null) {
/* 6110 */       sql.append(" AND DATEDIFF(NextContactDate, ?) >= 0 ");
/* 6111 */       params.add(start);
/*      */     }
/* 6113 */     if (end != null) {
/* 6114 */       sql.append(" AND DATEDIFF(NextContactDate, ?) <= 0 ");
/* 6115 */       params.add(end);
/*      */     }
/*      */ 
/* 6118 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 6120 */       Object obj = runner.query(conn, sql.toString(), 
/* 6121 */         new ScalarHandler(1), params.toArray());
/* 6122 */       if (!(obj instanceof Long)) {
/* 6123 */         return 0;
/*      */       }
/* 6125 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/* 6127 */       logger.warn("get need contact customer count failed", e);
/*      */     } finally {
/*      */       try {
/* 6130 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6132 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 6135 */     return 0;
/*      */   }
/*      */ 
/*      */   public int getContactedCustomerNum(String userId, int type, Date start, Date end)
/*      */   {
/* 6149 */     if ((userId == null) || (userId.length() == 0)) {
/* 6150 */       return 0;
/*      */     }
/* 6152 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6153 */     if (conn == null) {
/* 6154 */       return 0;
/*      */     }
/*      */ 
/* 6157 */     if ((start == null) && (end == null)) {
/* 6158 */       start = new Date();
/* 6159 */       end = new Date();
/*      */     }
/* 6161 */     StringBuffer sql = new StringBuffer(
/* 6162 */       "SELECT COUNT(*) FROM T_Customer_Info WHERE DeleteFlag = 0 AND Visible = ? AND PermissionsId = ?  ");
/*      */ 
/* 6164 */     List params = new ArrayList();
/*      */ 
/* 6166 */     params.add(Integer.valueOf(4));
/* 6167 */     params.add(userId);
/* 6168 */     if (type > -1) {
/* 6169 */       sql.append(" AND Type = ? ");
/* 6170 */       params.add(Integer.valueOf(type));
/*      */     }
/* 6172 */     if (start != null) {
/* 6173 */       sql.append(" AND DATEDIFF(LastContactDate, ?) >= 0 ");
/* 6174 */       params.add(start);
/*      */     }
/* 6176 */     if (end != null) {
/* 6177 */       sql.append(" AND DATEDIFF(LastContactDate, ?) <= 0 ");
/* 6178 */       params.add(end);
/*      */     }
/*      */ 
/* 6181 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 6183 */       Object obj = runner.query(conn, sql.toString(), 
/* 6184 */         new ScalarHandler(1), params.toArray());
/* 6185 */       if (!(obj instanceof Long)) {
/* 6186 */         return 0;
/*      */       }
/* 6188 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/* 6190 */       logger.warn("get contacted count failed", e);
/*      */     } finally {
/*      */       try {
/* 6193 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6195 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 6198 */     return 0;
/*      */   }
/*      */ 
/*      */   public void mergeCustomer(int oldId, int newId, boolean isPerson)
/*      */   {
/* 6208 */     if ((oldId <= 0) || (newId <= 0)) {
/* 6209 */       return;
/*      */     }
/* 6211 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6212 */     if (conn == null) {
/* 6213 */       return;
/*      */     }
/* 6215 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 6218 */       if (!isPerson) {
/*      */         try {
/* 6220 */           runner.update(conn, "UPDATE T_Customer_Info SET ParentCustomerId = ? WHERE ParentCustomerId = ?", new Object[] { Integer.valueOf(newId), 
/* 6221 */             Integer.valueOf(oldId) });
/*      */         } catch (SQLException e) {
/* 6223 */           logger.warn("merge customer failed", e);
/*      */         }
/*      */       }
/*      */       try
/*      */       {
/* 6228 */         runner.update(conn, "UPDATE T_Customer_Ext_Info SET CustomerId = ? WHERE CustomerId = ?", new Object[] { Integer.valueOf(newId), Integer.valueOf(oldId) });
/*      */       } catch (SQLException e) {
/* 6230 */         logger.warn("merge customer failed", e);
/*      */       }
/*      */ 
/*      */       try
/*      */       {
/* 6235 */         runner.update(conn, "UPDATE T_Note_Info SET SubjectId = ? WHERE SubjectId = ? AND SubjectType = ?", new Object[] { Integer.valueOf(newId), Integer.valueOf(oldId), 
/* 6236 */           Integer.valueOf(isPerson ? 1 : 0) });
/*      */       } catch (SQLException e) {
/* 6238 */         logger.warn("merge customer failed", e);
/*      */       }
/*      */       try
/*      */       {
/* 6242 */         runner.update(conn, "UPDATE T_Deal_Info SET CustomerId = ? WHERE CustomerId = ?", new Object[] { Integer.valueOf(newId), Integer.valueOf(oldId) });
/* 6243 */         runner.update(conn, "UPDATE T_Customer_Deal_Info SET CustomerId = ? WHERE CustomerId = ?", new Object[] { Integer.valueOf(newId), Integer.valueOf(oldId) });
/*      */       } catch (SQLException e) {
/* 6245 */         logger.warn("merge customer failed", e);
/*      */       }
/*      */ 
/*      */       try
/*      */       {
/* 6250 */         runner.update(conn, "UPDATE T_Customer_Tag_Info SET CustomerId = ? WHERE CustomerId = ?", new Object[] { Integer.valueOf(newId), Integer.valueOf(oldId) });
/*      */       } catch (SQLException e) {
/* 6252 */         logger.warn("merge customer failed", e);
/*      */       }
/*      */ 
/*      */       try
/*      */       {
/* 6258 */         runner.update(conn, "UPDATE T_Company_SMS_Msg_Info SET CustomerId = ? WHERE CustomerId = ?", new Object[] { Integer.valueOf(newId), Integer.valueOf(oldId) });
/* 6259 */         runner.update(conn, "UPDATE T_Company_SMS_Msg_Info SET ParentCustomerId = ? WHERE ParentCustomerId = ?", new Object[] { Integer.valueOf(newId), Integer.valueOf(oldId) });
/*      */       } catch (SQLException e) {
/* 6261 */         logger.warn("merge customer failed", e);
/*      */       }
/*      */ 
/*      */       try
/*      */       {
/* 6266 */         runner.update(conn, "DELETE FROM T_Customer_Permissions_Info WHERE CustomerId = ? ", Integer.valueOf(oldId));
/*      */       } catch (SQLException e) {
/* 6268 */         logger.warn("merge customer failed", e);
/*      */       }
/*      */ 
/*      */       try
/*      */       {
/* 6273 */         runner.update(conn, "UPDATE T_ImportantDate_Info SET CustomerId = ? WHERE CustomerId = ?", Integer.valueOf(oldId));
/*      */       } catch (SQLException e) {
/* 6275 */         logger.warn("merge customer failed", e);
/*      */       }
/*      */ 
/*      */       try
/*      */       {
/* 6280 */         runner.update(conn, "UPDATE T_Customer_Info SET DeleteFlag = 1, IndexFlag = 0 WHERE CompanyId = ? AND CustomerId = ? AND Visible = ? AND PermissionsId = ? ", Integer.valueOf(oldId));
/*      */       } catch (SQLException e) {
/* 6282 */         logger.warn("merge customer failed", e);
/*      */       }
/*      */     }
/*      */     finally {
/*      */       try {
/* 6287 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6289 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int changeMaxDayNotContactCustomersVisible(Connection conn, int companyId, int days, String categoryIds)
/*      */   {
/* 6304 */     if (days <= 0) {
/* 6305 */       return 0;
/*      */     }
/*      */ 
/* 6308 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 6310 */       CompanyExtManager manager = new CompanyExtManager();
/*      */ 
/* 6313 */       if ("1".equals(manager.getFieldValue(companyId, 
/* 6313 */         12)))
/*      */       {
/* 6317 */         String sql = "UPDATE T_Customer_Info SET PermissionsId = ?, Visible = ?, IndexFlag = 0, UpdateDate = NOW(), UpdateUserId = ? WHERE CompanyId = ? AND ((LastContactDate IS NULL AND (DATEDIFF(NOW(), AssignDate) > ?)) OR (DATEDIFF(NOW(), LastContactDate) > ?))";
/*      */ 
/* 6320 */         if (categoryIds
/* 6320 */           .indexOf("*") < 0) {
/* 6321 */           sql = sql + " AND CategoryId IN (" + categoryIds + ")";
/*      */         }
/* 6323 */         runner.update(conn, sql, new Object[] { "", Integer.valueOf(1), 
/* 6324 */           "system", Integer.valueOf(companyId), Integer.valueOf(days), Integer.valueOf(days) });
/*      */       }
/*      */       else
/*      */       {
/* 6330 */         String sql = "UPDATE T_Customer_Info SET PermissionsId = ?, Visible = ?, IndexFlag = 0, UpdateDate = NOW(), UpdateUserId = ? WHERE CompanyId = ? AND ((LastContactDate IS NULL AND (DATEDIFF(NOW(), AssignDate) > ?)) OR (DATEDIFF(NOW(), LastContactDate) > ?)) AND PermissionsId IN (SELECT UserId FROM T_User_Info WHERE GroupId = ? )";
/*      */ 
/* 6333 */         if (categoryIds
/* 6333 */           .indexOf("*") < 0) {
/* 6334 */           sql = sql + " AND CategoryId IN (" + categoryIds + ")";
/*      */         }
/*      */ 
/* 6337 */         GroupManager groupManager = new GroupManager();
/* 6338 */         List groups = groupManager.list(companyId);
/*      */ 
/* 6341 */         for (Object groupO : groups) {
                        Map group = (Map)groupO;
/* 6342 */           runner.update(conn, sql, new Object[] { group.get("GroupId"), 
/* 6343 */             Integer.valueOf(1), "system", 
/* 6344 */             Integer.valueOf(companyId), Integer.valueOf(days), Integer.valueOf(days), group.get("GroupId") });
/*      */         }
/*      */ 
/* 6348 */         runner.update(conn, sql, new Object[] { "", Integer.valueOf(1), 
/* 6349 */           "system", Integer.valueOf(companyId), Integer.valueOf(days), Integer.valueOf(days), Integer.valueOf(0) });
/*      */       }
/*      */     } catch (SQLException e) {
/* 6352 */       logger.warn(e);
/*      */     } catch (DBException ex) {
/* 6354 */       logger.warn(ex);
/*      */     }
/* 6356 */     return 0;
/*      */   }
/*      */ 
/*      */   public int deleteCustomerExtInfo(int customerExtId)
/*      */     throws DBException
/*      */   {
/* 6367 */     if (customerExtId <= 0) {
/* 6368 */       return 0;
/*      */     }
/* 6370 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6371 */     if (conn == null) {
/* 6372 */       throw new DBException();
/*      */     }
/* 6374 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 6377 */       return runner.update(conn, "DELETE FROM T_Customer_Ext_Info WHERE CustomerExtId = ?", 
/* 6378 */         Integer.valueOf(customerExtId));
/*      */     } catch (SQLException e) {
/* 6380 */       logger.warn("delete customer extInfo failed", e);
/*      */     } finally {
/*      */       try {
/* 6383 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6385 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 6388 */     return 0;
/*      */   }
/*      */ 
/*      */   public int updateCustomerExtInfo(String info1, String info2, String info3, String info4, int customerExtId)
/*      */     throws DBException
/*      */   {
/* 6400 */     if (customerExtId <= 0) {
/* 6401 */       return 0;
/*      */     }
/* 6403 */     if ((info1 == null) || (info1.trim().length() == 0)) {
/* 6404 */       return 0;
/*      */     }
/* 6406 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6407 */     if (conn == null) {
/* 6408 */       throw new DBException();
/*      */     }
/* 6410 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 6413 */       return runner.update(conn, "UPDATE T_Customer_Ext_Info SET Info1 = ?, Info2 = ?, Info3 = ?, Info4 = ? WHERE CustomerExtId = ?", new Object[] { info1, 
/* 6414 */         info2, info3, info4, Integer.valueOf(customerExtId) });
/*      */     } catch (SQLException e) {
/* 6416 */       logger.warn("update customer extInfo failed", e);
/*      */     } finally {
/*      */       try {
/* 6419 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6421 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 6424 */     return 0;
/*      */   }
/*      */ 
/*      */   public int addCustomerExtInfo(String info1, String info2, String info3, String info4, int customerId, int companyId, int filedId)
/*      */     throws DBException
/*      */   {
/* 6443 */     if ((customerId <= 0) || (companyId <= 0) || (filedId < 0)) {
/* 6444 */       return 0;
/*      */     }
/* 6446 */     if ((info1 == null) || (info1.trim().length() == 0)) {
/* 6447 */       return 0;
/*      */     }
/* 6449 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6450 */     if (conn == null) {
/* 6451 */       throw new DBException();
/*      */     }
/* 6453 */     GenKeyQueryRunner runner = new GenKeyQueryRunner(
/* 6454 */       new ScalarHandler(), new String[] { "CustomerExtId" });
/*      */     try {
/* 6456 */       runner.update(conn, "INSERT INTO T_Customer_Ext_Info (CompanyId,CustomerId,Type,Info1,Info2,Info3,Info4,CreateDate) VALUES (?,?,?,?,?,?,?,NOW())", new Object[] { Integer.valueOf(companyId), 
/* 6457 */         Integer.valueOf(customerId), Integer.valueOf(filedId), info1, info2, info3, info4 });
/* 6458 */       return ((Long)runner.getGeneratedKeys()).intValue();
/*      */     } catch (SQLException e) {
/* 6460 */       logger.warn("add customer extInfo failed", e);
/*      */     } finally {
/*      */       try {
/* 6463 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6465 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 6468 */     return 0;
/*      */   }
/*      */ 
/*      */   public int updateCustomerName(int customerId, String name, String userId)
/*      */   {
/* 6480 */     if (customerId <= 0) {
/* 6481 */       return 0;
/*      */     }
/* 6483 */     if ((name == null) || (name.trim().length() == 0)) {
/* 6484 */       return 0;
/*      */     }
/* 6486 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6487 */     if (conn == null)
/* 6488 */       return 0;
/*      */     try
/*      */     {
/* 6491 */       QueryRunner runner = new QueryRunner();
/* 6492 */       return runner.update(conn, "UPDATE T_Customer_Info SET Name = ?, UpdateDate = NOW(), UpdateUserId = ? WHERE CustomerId = ?", new Object[] { name, 
/* 6493 */         userId, Integer.valueOf(customerId) });
/*      */     } catch (SQLException e) {
/* 6495 */       logger.warn("update img path failed", e);
/*      */     } finally {
/*      */       try {
/* 6498 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6500 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 6503 */     return 0;
/*      */   }
/*      */ 
/*      */   public int updateCustomerTitle(int customerId, String title, String userId)
/*      */   {
/* 6515 */     if (customerId <= 0) {
/* 6516 */       return 0;
/*      */     }
/* 6518 */     if (title == null) {
/* 6519 */       title = "";
/*      */     }
/* 6521 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6522 */     if (conn == null)
/* 6523 */       return 0;
/*      */     try
/*      */     {
/* 6526 */       QueryRunner runner = new QueryRunner();
/* 6527 */       return runner.update(conn, "UPDATE T_Customer_Info SET Title = ?, UpdateDate = NOW(), UpdateUserId = ? WHERE CustomerId = ?", new Object[] { title, 
/* 6528 */         userId, Integer.valueOf(customerId) });
/*      */     } catch (SQLException e) {
/* 6530 */       logger.warn("update img path failed", e);
/*      */     } finally {
/*      */       try {
/* 6533 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6535 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 6538 */     return 0;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getPeoplePhones(int id)
/*      */     throws SQLException
/*      */   {
/* 6551 */     if (id <= 0) {
/* 6552 */       return null;
/*      */     }
/* 6554 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6555 */     if (conn == null) {
/* 6556 */       return null;
/*      */     }
/* 6558 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 6560 */       return (List)runner.query(conn, "SELECT A.Info1 AS Phone, B.Name FROM T_Customer_Ext_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE A.Type = ? AND A.CustomerId = ? ", 
/* 6561 */         new MyMapListHandler(), new Object[] { Integer.valueOf(1), 
/* 6562 */         Integer.valueOf(id) });
/*      */     } finally {
/*      */       try {
/* 6565 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6567 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCompanyPhones(int id)
/*      */     throws SQLException
/*      */   {
/* 6581 */     if (id <= 0) {
/* 6582 */       return null;
/*      */     }
/* 6584 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6585 */     if (conn == null) {
/* 6586 */       return null;
/*      */     }
/* 6588 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 6590 */       return (List)runner.query(conn, "SELECT A.Info1 AS Phone, B.Name FROM T_Customer_Ext_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE A.Type = ? AND ( A.CustomerId = ?  OR A.CustomerId IN (SELECT CustomerId FROM T_Customer_Info WHERE ParentCustomerId = ?))", 
/* 6591 */         new MyMapListHandler(), new Object[] { Integer.valueOf(1), 
/* 6592 */         Integer.valueOf(id), Integer.valueOf(id) });
/*      */     } finally {
/*      */       try {
/* 6595 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6597 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCompanyPeoples(int id)
/*      */   {
/* 6609 */     if (id <= 0) {
/* 6610 */       return null;
/*      */     }
/* 6612 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6613 */     if (conn == null) {
/* 6614 */       return null;
/*      */     }
/*      */ 
/* 6617 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 6619 */       return (List)runner.query(conn, "SELECT CustomerId, Name, Title, CreateUserId FROM T_Customer_Info WHERE DeleteFlag = 0 AND ParentCustomerId  = ? ORDER BY CustomerId DESC ", 
/* 6620 */         new MyMapListHandler(), new Object[] { Integer.valueOf(id) });
/*      */     } catch (SQLException e) {
/* 6622 */       logger.warn("get company's peoples info failed", e);
/*      */     } finally {
/*      */       try {
/* 6625 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6627 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 6630 */     return null;
/*      */   }
/*      */ 
/*      */   public CustomerInfo getCustomerRightInfo(int id)
/*      */   {
/* 6640 */     CustomerInfo customer = new CustomerInfo();
/*      */ 
/* 6642 */     if (id <= 0) {
/* 6643 */       return null;
/*      */     }
/* 6645 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6646 */     if (conn == null) {
/* 6647 */       return null;
/*      */     }
/*      */ 
/* 6650 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 6652 */       Map result = (Map)runner.query(conn, 
/* 6653 */         "SELECT Type, Visible,PermissionsId,ParentCustomerId FROM T_Customer_Info WHERE CustomerId = ?", new MyMapHandler(), new Object[] { Integer.valueOf(id) });
/*      */ 
/* 6655 */       if ((result == null) || (result.size() == 0)) {
/* 6656 */         return null;
/*      */       }
/*      */ 
/* 6659 */       customer.visible = Util.str2Int(result.get("Visible"));
/* 6660 */       customer.permissionsId = ((String)result.get("PermissionsId"));
/*      */ 
/* 6663 */       if (customer.visible == 4) {
/* 6664 */         if (customer.isCompany()) {
/* 6665 */           getShareInfo(conn, customer, customer.id);
/*      */         }
/* 6668 */         else if (customer.parentId == -1)
/*      */         {
/* 6670 */           getShareInfo(conn, customer, customer.id);
/*      */         }
/* 6672 */         else getShareInfo(conn, customer, customer.parentId);
/*      */       }
/*      */ 
/*      */     }
/*      */     catch (SQLException e)
/*      */     {
/* 6678 */       logger.warn("get company's peoples info failed", e);
/*      */       try
/*      */       {
/* 6681 */         DbUtils.close(conn);
/*      */       } catch (SQLException se) {
/* 6683 */         logger.warn("close conn failed", se);
/*      */       }
/*      */     }
/*      */     finally
/*      */     {
/*      */       try
/*      */       {
/* 6681 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6683 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/* 6687 */     return customer;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerName(int companyId, String ids)
/*      */   {
/* 6698 */     if (companyId < 0) {
/* 6699 */       return null;
/*      */     }
/* 6701 */     if ((ids == null) || (ids.trim().length() == 0)) {
/* 6702 */       return null;
/*      */     }
/* 6704 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6705 */     if (conn == null) {
/* 6706 */       return null;
/*      */     }
/*      */ 
/* 6709 */     String sql = "SELECT CustomerId, Name, Type FROM T_Customer_Info WHERE CompanyId = ? AND DeleteFlag = 0 AND CustomerId IN ( " + ids + " ) ";
/*      */ 
/* 6711 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 6713 */       return (List)runner.query(conn, sql, new MyMapListHandler(), new Object[] { Integer.valueOf(companyId) });
/*      */     } catch (SQLException e) {
/* 6715 */       logger.warn("get customers name failed", e);
/*      */     } finally {
/*      */       try {
/* 6718 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6720 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 6723 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getNeedIndexCustomers(Connection conn)
/*      */   {
/* 6732 */     if (conn == null) {
/* 6733 */       return null;
/*      */     }
/*      */ 
/* 6736 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 6738 */       return (List)runner.query(conn, "SELECT CustomerId, Type, Name, Title, Visible, PermissionsId, DeleteFlag FROM T_Customer_Info WHERE IndexFlag = 0 LIMIT 0, 100", 
/* 6739 */         new MyMapListHandler());
/*      */     } catch (SQLException e) {
/* 6741 */       logger.warn("get customers name failed", e);
/*      */     }
/*      */ 
/* 6744 */     return null;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getNeedIndexCustomersExtInfo(Connection conn, String ids)
/*      */   {
/* 6756 */     if (conn == null) {
/* 6757 */       return null;
/*      */     }
/*      */ 
/* 6760 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 6762 */       return (List)runner.query(conn, "SELECT CustomerId, Info1 FROM T_Customer_Ext_Info WHERE CustomerId IN (" + ids + 
/* 6763 */         ") ORDER BY CustomerId DESC ", new MyMapListHandler());
/*      */     } catch (SQLException e) {
/* 6765 */       logger.warn("get customer index extinfo failed", e);
/*      */     }
/*      */ 
/* 6768 */     return null;
/*      */   }
/*      */ 
/*      */   public void updateIndexFlag(Connection conn, String ids)
/*      */   {
/* 6778 */     if ((conn == null) || (ids == null) || (ids.trim().length() == 0))
/* 6779 */       return;
/*      */     try
/*      */     {
/* 6782 */       QueryRunner runner = new QueryRunner();
/* 6783 */       runner.update(conn, "UPDATE T_Customer_Info SET IndexFlag = 1 WHERE CustomerId IN (" + ids + ")");
/*      */     } catch (SQLException e) {
/* 6785 */       logger.warn("update customer index flag failed", e);
/*      */     }
/*      */   }
/*      */ 
/*      */   public int clearIndexFlag(Connection conn)
/*      */   {
/* 6796 */     if (conn == null)
/* 6797 */       return 0;
/*      */     try
/*      */     {
/* 6800 */       QueryRunner runner = new QueryRunner();
/* 6801 */       return runner.update(conn, "UPDATE T_Customer_Info SET IndexFlag = 0 ");
/*      */     } catch (SQLException e) {
/* 6803 */       logger.warn("clear customer index flag failed", e);
/*      */     }
/* 6805 */     return 0;
/*      */   }
/*      */ 
/*      */   public long getTotalNum(Connection conn, int companyId, int type)
/*      */   {
/* 6817 */     if (conn == null) {
/* 6818 */       return 0L;
/*      */     }
/* 6820 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 6823 */       Object obj = runner
/* 6824 */         .query(conn, 
/* 6825 */         "SELECT COUNT(*) FROM T_Customer_Info WHERE CompanyId = ? AND Type = ? ", 
/* 6826 */         new ScalarHandler(1), new Object[] { Integer.valueOf(companyId), Integer.valueOf(type) });
/* 6827 */       if (!(obj instanceof Long)) {
/* 6828 */         return 0L;
/*      */       }
/* 6830 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/* 6832 */       logger.warn("get customer total num failed", e);
/*      */     }
/* 6834 */     return 0L;
/*      */   }
/*      */ 
/*      */   public List<Map<String, Object>> getCustomerNameByIds(int companyId, String ids)
/*      */   {
/* 6846 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6847 */     if (conn == null) {
/* 6848 */       return null;
/*      */     }
/*      */ 
/* 6851 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 6854 */       return (List)runner.query(conn, "SELECT CustomerId, Name FROM T_Customer_Info WHERE CompanyId = ? AND CustomerId IN (" + ids + 
/* 6855 */         ")", new MapListHandler(), new Object[] { Integer.valueOf(companyId) });
/*      */     } catch (SQLException e) {
/* 6857 */       logger.warn("get customer name failed by ids", e);
/*      */     } finally {
/*      */       try {
/* 6860 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6862 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/* 6866 */     return null;
/*      */   }
/*      */ 
/*      */   public int moveToCompanyPublicDate(int companyId)
/*      */   {
/* 6876 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 6877 */     if (conn == null) {
/* 6878 */       return 0;
/*      */     }
/*      */     try
/*      */     {
/* 6882 */       QueryRunner runner = new QueryRunner();
/* 6883 */       return runner.update(conn, "UPDATE T_Customer_Info SET PermissionsId = '' WHERE CompanyId = ? AND Visible = ? ", new Object[] { 
/* 6884 */         Integer.valueOf(companyId), Integer.valueOf(1) });
/*      */     }
/*      */     catch (SQLException e) {
/* 6887 */       logger.warn("clear customer index flag failed", e);
/*      */     } finally {
/*      */       try {
/* 6890 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 6892 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/* 6895 */     return 0;
/*      */   }
/*      */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.db.CustomerManager
 * JD-Core Version:    0.6.1
 */